| Conditions | 12 |
| Paths | 128 |
| Total Lines | 54 |
| Code Lines | 32 |
| 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 |
||
| 90 | private function createExclusionStrategies(FOSContext $context) |
||
| 91 | { |
||
| 92 | $exclusionStrategies = []; |
||
| 93 | |||
| 94 | $groups = $context->getGroups(); |
||
| 95 | $version = $context->getVersion(); |
||
| 96 | |||
| 97 | if (!empty($groups)) { |
||
| 98 | $exclusionStrategies[] = new GroupsExclusionStrategy($groups); |
||
| 99 | } |
||
| 100 | |||
| 101 | if (!empty($version)) { |
||
| 102 | $exclusionStrategies[] = new VersionExclusionStrategy($version); |
||
| 103 | } |
||
| 104 | |||
| 105 | if (method_exists($context, 'isMaxDepthEnabled')) { |
||
| 106 | if ($context->isMaxDepthEnabled() !== null) { |
||
| 107 | $exclusionStrategies[] = new MaxDepthExclusionStrategy(); |
||
| 108 | } |
||
| 109 | } else { |
||
| 110 | $maxDepth = $context->getMaxDepth(); |
||
| 111 | |||
| 112 | if (!empty($maxDepth)) { |
||
| 113 | $exclusionStrategies[] = new MaxDepthExclusionStrategy($maxDepth); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | $customExclusionStrategies = $context->getAttribute($attribute = 'ivory_exclusion_strategies') ?: []; |
||
| 118 | |||
| 119 | if (!is_array($customExclusionStrategies) && !$customExclusionStrategies instanceof \Traversable) { |
||
| 120 | throw new \RuntimeException(sprintf( |
||
| 121 | 'The "%s" context attribute must be an array or implement "%s".', |
||
| 122 | $attribute, |
||
| 123 | \Traversable::class |
||
| 124 | )); |
||
| 125 | } |
||
| 126 | |||
| 127 | foreach ($customExclusionStrategies as $customExclusionStrategy) { |
||
| 128 | if (!$customExclusionStrategy instanceof ExclusionStrategyInterface) { |
||
| 129 | throw new \RuntimeException(sprintf( |
||
| 130 | 'The "%s" context attribute must be an array of "%s", got "%s".', |
||
| 131 | $attribute, |
||
| 132 | ExclusionStrategyInterface::class, |
||
| 133 | is_object($customExclusionStrategy) |
||
| 134 | ? get_class($customExclusionStrategy) |
||
| 135 | : gettype($customExclusionStrategy) |
||
| 136 | )); |
||
| 137 | } |
||
| 138 | |||
| 139 | $exclusionStrategies[] = $customExclusionStrategy; |
||
| 140 | } |
||
| 141 | |||
| 142 | return $exclusionStrategies; |
||
| 143 | } |
||
| 144 | } |
||
| 145 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: