| Conditions | 9 |
| Paths | 8 |
| Total Lines | 55 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 69 | public function resolve($name) |
||
| 70 | { |
||
| 71 | $collections = $this->getCollections(); |
||
| 72 | |||
| 73 | if (!isset($collections[$name])) { |
||
| 74 | return null; |
||
| 75 | } |
||
| 76 | |||
| 77 | if (!is_array($collections[$name])) { |
||
| 78 | throw new Exception\RuntimeException( |
||
| 79 | "Collection with name $name is not an an array." |
||
| 80 | ); |
||
| 81 | } |
||
| 82 | |||
| 83 | $collection = new AssetCollection; |
||
| 84 | $mimeType = null; |
||
| 85 | $collection->setTargetPath($name); |
||
| 86 | |||
| 87 | foreach ($collections[$name] as $asset) { |
||
| 88 | if (!is_string($asset)) { |
||
| 89 | throw new Exception\RuntimeException( |
||
| 90 | 'Asset should be of type string. got ' . gettype($asset) |
||
| 91 | ); |
||
| 92 | } |
||
| 93 | |||
| 94 | if (null === ($res = $this->getAggregateResolver()->resolve($asset))) { |
||
| 95 | throw new Exception\RuntimeException("Asset '$asset' could not be found."); |
||
| 96 | } |
||
| 97 | |||
| 98 | if (!$res instanceof AssetInterface) { |
||
| 99 | throw new Exception\RuntimeException( |
||
| 100 | "Asset '$asset' does not implement Assetic\\Asset\\AssetInterface." |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | if (null !== $mimeType && $res->mimetype !== $mimeType) { |
||
|
|
|||
| 105 | throw new Exception\RuntimeException(sprintf( |
||
| 106 | 'Asset "%s" from collection "%s" doesn\'t have the expected mime-type "%s".', |
||
| 107 | $asset, |
||
| 108 | $name, |
||
| 109 | $mimeType |
||
| 110 | )); |
||
| 111 | } |
||
| 112 | |||
| 113 | $mimeType = $res->mimetype; |
||
| 114 | |||
| 115 | $this->getAssetFilterManager()->setFilters($asset, $res); |
||
| 116 | |||
| 117 | $collection->add($res); |
||
| 118 | } |
||
| 119 | |||
| 120 | $collection->mimetype = $mimeType; |
||
| 121 | |||
| 122 | return $collection; |
||
| 123 | } |
||
| 124 | |||
| 153 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: