We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 8 |
| Paths | 1 |
| Total Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 66 |
| CRAP Score | 8 |
| 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 |
||
| 22 | public function getConfigTreeBuilder() |
||
| 23 | { |
||
| 24 | 37 | $treeBuilder = new TreeBuilder(); |
|
| 25 | $rootNode = $treeBuilder->root('overblog_graphql_types'); |
||
| 26 | 37 | ||
| 27 | 37 | $configTypeKeys = \array_map( |
|
| 28 | function ($type) { |
||
| 29 | 37 | return $this->normalizedConfigTypeKey($type); |
|
| 30 | }, |
||
| 31 | 37 | self::$types |
|
| 32 | 37 | ); |
|
| 33 | 37 | ||
| 34 | $this->addBeforeNormalization($rootNode); |
||
| 35 | |||
| 36 | 37 | $rootNode |
|
|
|
|||
| 37 | ->useAttributeAsKey('name') |
||
| 38 | ->prototype('array') |
||
| 39 | 37 | // config is the unique config entry allowed |
|
| 40 | 37 | ->beforeNormalization() |
|
| 41 | ->ifTrue(function ($v) use ($configTypeKeys) { |
||
| 42 | 37 | if (!empty($v) && \is_array($v)) { |
|
| 43 | $keys = \array_keys($v); |
||
| 44 | 37 | foreach ($configTypeKeys as $configTypeKey) { |
|
| 45 | 37 | if (\in_array($configTypeKey, $keys)) { |
|
| 46 | 37 | return true; |
|
| 47 | 37 | } |
|
| 48 | 37 | } |
|
| 49 | } |
||
| 50 | |||
| 51 | return false; |
||
| 52 | }) |
||
| 53 | 32 | ->thenInvalid( |
|
| 54 | 37 | \sprintf( |
|
| 55 | 37 | 'Don\'t use internal config keys %s, replace it by "config" instead.', |
|
| 56 | 37 | \implode(', ', $configTypeKeys) |
|
| 57 | 37 | ) |
|
| 58 | 37 | ) |
|
| 59 | ->end() |
||
| 60 | // config is renamed _{TYPE}_config |
||
| 61 | 37 | ->beforeNormalization() |
|
| 62 | ->ifTrue(function ($v) { |
||
| 63 | 37 | return isset($v['type']) && \is_string($v['type']); |
|
| 64 | }) |
||
| 65 | 32 | ->then(function ($v) { |
|
| 66 | 37 | $key = $this->normalizedConfigTypeKey($v['type']); |
|
| 67 | |||
| 68 | 32 | if (empty($v[$key])) { |
|
| 69 | $v[$key] = isset($v['config']) ? $v['config'] : []; |
||
| 70 | 32 | } |
|
| 71 | 32 | unset($v['config']); |
|
| 72 | |||
| 73 | 32 | return $v; |
|
| 74 | }) |
||
| 75 | 32 | ->end() |
|
| 76 | 37 | ->cannotBeOverwritten() |
|
| 77 | 37 | ->children() |
|
| 78 | 37 | ->scalarNode('class_name') |
|
| 79 | 37 | ->isRequired() |
|
| 80 | 37 | ->validate() |
|
| 81 | 37 | ->ifTrue(function ($name) { |
|
| 82 | 37 | return !\preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name); |
|
| 83 | }) |
||
| 84 | 32 | ->thenInvalid('A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.') |
|
| 85 | 37 | ->end() |
|
| 86 | 37 | ->end() |
|
| 87 | 37 | ->enumNode('type')->values(self::$types)->isRequired()->end() |
|
| 88 | 37 | ->arrayNode(InheritanceProcessor::INHERITS_KEY) |
|
| 89 | 37 | ->prototype('scalar')->info('Types to inherit of.')->end() |
|
| 90 | 37 | ->end() |
|
| 91 | 37 | ->booleanNode('decorator')->info('Decorator will not be generated.')->defaultFalse()->end() |
|
| 92 | 37 | ->append(Config\ObjectTypeDefinition::create()->getDefinition()) |
|
| 93 | 37 | ->append(Config\EnumTypeDefinition::create()->getDefinition()) |
|
| 94 | 37 | ->append(Config\InterfaceTypeDefinition::create()->getDefinition()) |
|
| 95 | 37 | ->append(Config\UnionTypeDefinition::create()->getDefinition()) |
|
| 96 | 37 | ->append(Config\InputObjectTypeDefinition::create()->getDefinition()) |
|
| 97 | 37 | ->append(Config\CustomScalarTypeDefinition::create()->getDefinition()) |
|
| 98 | 37 | ->variableNode('config')->end() |
|
| 99 | 37 | ->end() |
|
| 100 | 37 | // _{TYPE}_config is renamed config |
|
| 101 | 37 | ->validate() |
|
| 102 | ->ifTrue(function ($v) { |
||
| 103 | 37 | return isset($v[$this->normalizedConfigTypeKey($v['type'])]); |
|
| 104 | }) |
||
| 105 | 32 | ->then(function ($v) { |
|
| 106 | 37 | $key = $this->normalizedConfigTypeKey($v['type']); |
|
| 107 | $v['config'] = $v[$key]; |
||
| 108 | 32 | unset($v[$key]); |
|
| 109 | 32 | ||
| 110 | 32 | return $v; |
|
| 111 | }) |
||
| 112 | 32 | ->end() |
|
| 113 | 37 | ||
| 114 | 37 | ->end(); |
|
| 115 | |||
| 116 | 37 | return $treeBuilder; |
|
| 117 | } |
||
| 118 | 37 | ||
| 139 |
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: