Conditions | 10 |
Paths | 2 |
Total Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Tests | 20 |
CRAP Score | 11.7434 |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
33 | 1 | public function format(Asset $asset, string $format = '%url%', bool $absolute = false): string |
|
34 | { |
||
35 | 1 | $base = $absolute ? $this->baseUrl : $this->basePath; |
|
36 | |||
37 | 1 | return Strings::replace($format, |
|
38 | 1 | '/%([^%]+)%/', |
|
39 | 1 | function ($matches) use ($asset, $format, $base) { |
|
40 | 1 | switch ($matches[1]) { |
|
41 | case 'content': |
||
42 | 1 | $content = file_get_contents($asset->getAbsolutePath()); |
|
43 | 1 | return $content ? trim($content) : ''; |
|
44 | case 'raw': |
||
45 | 1 | return $asset->getRevision()->getRawValue(); |
|
46 | case 'base': |
||
47 | 1 | return $base; |
|
48 | case 'basePath': |
||
49 | 1 | return $this->basePath; |
|
50 | case 'baseUrl': |
||
51 | 1 | return $this->baseUrl; |
|
52 | case 'path': |
||
53 | 1 | return $asset->getRelativePath(); |
|
54 | case 'url': |
||
55 | 1 | return sprintf('%s%s', $base, $asset->getRelativeUrl()); |
|
56 | default: |
||
57 | 1 | $msg = sprintf( |
|
58 | "Asset macro: Invalid variable '%s' in format '%s'. " . |
||
59 | 1 | 'Use one of allowed variables: %%raw%%, %%basePath%%, %%path%%, %%url%%.', |
|
60 | 1 | $matches[1], |
|
61 | 1 | $format |
|
62 | ); |
||
63 | 1 | throw new InvalidVariableException($msg); |
|
64 | } |
||
65 | 1 | }); |
|
66 | } |
||
67 | } |
||
68 |