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 | 82 |
Code Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Tests | 59 |
CRAP Score | 8 |
Changes | 3 | ||
Bugs | 1 | Features | 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 |
||
33 | 15 | public function getConfigTreeBuilder() |
|
34 | { |
||
35 | 15 | $treeBuilder = new TreeBuilder(); |
|
36 | 15 | $rootNode = $treeBuilder->root('overblog_graphql_types'); |
|
37 | |||
38 | 15 | $configTypeKeys = array_map( |
|
39 | function ($type) { |
||
40 | 15 | return $this->normalizedConfigTypeKey($type); |
|
41 | 15 | }, |
|
42 | static::$types |
||
|
|||
43 | 15 | ); |
|
44 | |||
45 | 15 | $this->addBeforeNormalization($rootNode); |
|
46 | |||
47 | $rootNode |
||
48 | 15 | ->useAttributeAsKey('name') |
|
49 | 15 | ->prototype('array') |
|
50 | // config is the unique config entry allowed |
||
51 | 15 | ->beforeNormalization() |
|
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(static::$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 | |||
181 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: