Complex classes like AssetMacro often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AssetMacro, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | 1 | class AssetMacro extends MacroSet |
|
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Name of Latte provider of macro configuration |
||
| 23 | */ |
||
| 24 | const CONFIG_PROVIDER = 'assetMacroConfig'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Memory cache for decoded JSON content of revisions manifests (path => content) |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private static $manifestCache = []; |
||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * @param Latte\Compiler $compiler |
||
| 35 | */ |
||
| 36 | 1 | public static function install(Latte\Compiler $compiler) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @param Latte\MacroNode $node |
||
| 44 | * @param Latte\PhpWriter $writer |
||
| 45 | * @return string |
||
| 46 | * @throws Latte\CompileException |
||
| 47 | */ |
||
| 48 | 1 | public function macroAsset(Latte\MacroNode $node, Latte\PhpWriter $writer) |
|
| 49 | { |
||
| 50 | 1 | if ($node->modifiers && $node->modifiers != '|noescape') { |
|
| 51 | 1 | throw new Latte\CompileException('Only \'noescape\' modifier is allowed in ' . $node->getNotation()); |
|
| 52 | } |
||
| 53 | |||
| 54 | // Validate arguments count |
||
| 55 | 1 | $args = trim($node->args); |
|
| 56 | 1 | $argsCount = $args === '' ? 0 : (substr_count($args, ',') + 1); |
|
| 57 | 1 | if ($argsCount === 0) { |
|
| 58 | 1 | throw new Latte\CompileException("Asset macro requires at least one argument."); |
|
| 59 | 1 | } else if ($argsCount > 3) { |
|
| 60 | 1 | throw new Latte\CompileException("Asset macro must have no more than 3 arguments."); |
|
| 61 | } |
||
| 62 | |||
| 63 | 1 | return $writer->write( |
|
| 64 | 1 | 'echo ' . ($node->modifiers !== '|noescape' ? '%escape' : '') . |
|
| 65 | 1 | '(' . self::class . '::getOutput(' . |
|
| 66 | 1 | '%node.word, ' . |
|
| 67 | 1 | '%node.array, ' . |
|
| 68 | 1 | '$basePath, ' . |
|
| 69 | 1 | '$this->global->' . self::CONFIG_PROVIDER . ', ' . |
|
| 70 | 1 | 'isset($this->global->cacheStorage) ? $this->global->cacheStorage : null))'); |
|
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param string $asset Asset relative path |
||
| 75 | * @param array $args Other macro arguments |
||
| 76 | * @param string $basePath Base path |
||
| 77 | * @param array $config Macro configuration |
||
| 78 | * @param IStorage $storage Cache storage |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | 1 | public static function getOutput($asset, array $args, $basePath, array $config, IStorage $storage = null) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $asset Asset relative path |
||
| 102 | * @param array $args Other macro arguments |
||
| 103 | * @param string $basePath Base path |
||
| 104 | * @param array $config Macro configuration |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 1 | public static function generateOutput($asset, array $args, $basePath, array $config) |
|
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $asset Asset path specified in macro |
||
| 127 | * @param array $args Macro arguments |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | 1 | private static function processArguments($asset, array $args) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @param string $relativePath Relative asset path |
||
| 146 | * @param string $needed Fail if manifest doesn't exist? |
||
| 147 | * @param array $config Macro configuration |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | 1 | private static function getRevision($relativePath, $needed, array $config) |
|
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $asset Asset path specified in macro |
||
| 179 | * @param bool $needed Fail if manifest doesn't exist? |
||
| 180 | * @param string $wwwDir Public www dir |
||
| 181 | * @param array $config Macro configuration |
||
| 182 | * @return null|array |
||
| 183 | */ |
||
| 184 | 1 | private static function getManifest($asset, $needed, $wwwDir, array $config) |
|
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $asset Asset path specified in macro |
||
| 213 | * @param string $wwwDir Public www dir |
||
| 214 | * @param bool $needed Fail if asset/manifest doesn't exist? |
||
| 215 | * @param array $config Macro configuration |
||
| 216 | * @return null|array |
||
| 217 | */ |
||
| 218 | 1 | private static function autodetectManifest($asset, $wwwDir, $needed, array $config) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $format Output format |
||
| 249 | * @param string $absolutePath Absolute asset path |
||
| 250 | * @param string $relativePath Asset relative path |
||
| 251 | * @param string $basePath Base path |
||
| 252 | * @param string|null $revision Asset revision (version or path to file) |
||
| 253 | * @param bool $revisionIsVersion Is revision only version or full path? |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | private static function formatOutput($format, $absolutePath, $relativePath, $basePath, $revision, $revisionIsVersion) |
||
| 288 | } |
||
| 289 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: