We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 9 |
| Paths | 1 |
| Total Lines | 79 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 62 |
| CRAP Score | 9.0003 |
| Changes | 4 | ||
| Bugs | 1 | Features | 2 |
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 |
||
| 52 | ->ifTrue(function ($v) use ($configTypeKeys) { |
||
| 53 | 15 | if (!empty($v) && is_array($v)) { |
|
| 54 | 15 | $keys = array_keys($v); |
|
| 55 | 15 | foreach ($configTypeKeys as $configTypeKey) { |
|
| 56 | 15 | if (in_array($configTypeKey, $keys)) { |
|
| 57 | 5 | return true; |
|
| 58 | } |
||
| 59 | 14 | } |
|
| 60 | 10 | } |
|
| 61 | |||
| 62 | 10 | return false; |
|
| 63 | 15 | }) |
|
| 64 | 15 | ->thenInvalid( |
|
| 65 | 15 | sprintf( |
|
| 66 | 15 | 'Don\'t use internal config keys %s, replace it by "config" instead.', |
|
| 67 | 15 | implode(', ', $configTypeKeys) |
|
| 68 | 15 | ) |
|
| 69 | 15 | ) |
|
| 70 | 15 | ->end() |
|
| 71 | // config is renamed _{TYPE}_config |
||
| 72 | 15 | ->beforeNormalization() |
|
| 73 | ->ifTrue(function ($v) { |
||
| 74 | 10 | return isset($v['type']) && is_string($v['type']); |
|
| 75 | 15 | }) |
|
| 76 | ->then(function ($v) { |
||
| 77 | 9 | $key = $this->normalizedConfigTypeKey($v['type']); |
|
| 78 | |||
| 79 | 9 | if (empty($v[$key])) { |
|
| 80 | 9 | $v[$key] = isset($v['config']) ? $v['config'] : []; |
|
| 81 | 9 | } |
|
| 82 | 9 | unset($v['config']); |
|
| 83 | |||
| 84 | 9 | return $v; |
|
| 85 | 15 | }) |
|
| 86 | 15 | ->end() |
|
| 87 | 15 | ->cannotBeOverwritten() |
|
| 88 | 15 | ->children() |
|
| 89 | 15 | ->enumNode('type')->values(self::$types)->isRequired()->end() |
|
| 90 | 15 | ->append(Config\ObjectTypeDefinition::create()->getDefinition()) |
|
| 91 | 15 | ->append(Config\EnumTypeDefinition::create()->getDefinition()) |
|
| 92 | 15 | ->append(Config\InterfaceTypeDefinition::create()->getDefinition()) |
|
| 93 | 15 | ->append(Config\UnionTypeDefinition::create()->getDefinition()) |
|
| 94 | 15 | ->append(Config\InputObjectTypeDefinition::create()->getDefinition()) |
|
| 95 | 15 | ->variableNode('config')->end() |
|
| 96 | 15 | ->end() |
|
| 97 | // _{TYPE}_config is renamed config |
||
| 98 | 15 | ->validate() |
|
| 99 | ->ifTrue(function ($v) { |
||
| 100 | 9 | return isset($v[$this->normalizedConfigTypeKey($v['type'])]); |
|
| 101 | 15 | }) |
|
| 102 | ->then(function ($v) { |
||
| 103 | 9 | $key = $this->normalizedConfigTypeKey($v['type']); |
|
| 104 | 9 | $v['config'] = $v[$key]; |
|
| 105 | 9 | unset($v[$key]); |
|
| 106 | |||
| 107 | 9 | return $v; |
|
| 108 | 15 | }) |
|
| 109 | 15 | ->end() |
|
| 110 | |||
| 111 | 15 | ->end(); |
|
| 112 | |||
| 113 | 15 | return $treeBuilder; |
|
| 114 | } |
||
| 115 | |||
| 116 | 15 | private function addBeforeNormalization(ArrayNodeDefinition $node) |
|
| 117 | { |
||
| 118 | $typeKeyExists = function ($types) { return !empty($types) && is_array($types); }; |
||
| 119 | |||
| 120 | $node |
||
| 121 | // set type config.name |
||
| 122 | 15 | ->beforeNormalization() |
|
| 123 | 15 | ->ifTrue($typeKeyExists) |
|
| 124 | ->then(function ($types) { |
||
| 125 | 15 | foreach ($types as $name => &$type) { |
|
| 126 | 15 | $type['config'] = isset($type['config']) && is_array($type['config']) ? $type['config'] : []; |
|
| 127 | 15 | $type['config']['name'] = $name; |
|
| 128 | 15 | } |
|
| 129 | |||
| 130 | 15 | return $types; |
|
| 131 | 15 | }) |
|
| 181 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: