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 |
| Code Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 67 |
| 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 |
||
| 34 | public function getConfigTreeBuilder() |
||
| 35 | 47 | { |
|
| 36 | $treeBuilder = new TreeBuilder('overblog_graphql_types'); |
||
| 37 | |||
| 38 | 47 | /** @var ArrayNodeDefinition $rootNode */ |
|
| 39 | $rootNode = $treeBuilder->getRootNode(); |
||
| 40 | 47 | ||
| 41 | $configTypeKeys = array_map(fn ($type) => $this->normalizedConfigTypeKey($type), self::$types); |
||
| 42 | 47 | ||
| 43 | $this->addBeforeNormalization($rootNode); |
||
| 44 | |||
| 45 | // @phpstan-ignore-next-line |
||
| 46 | 47 | $rootNode |
|
| 47 | 47 | ->useAttributeAsKey('name') |
|
| 48 | ->prototype('array') |
||
| 49 | 47 | # config is the unique config entry allowed |
|
| 50 | 47 | ->beforeNormalization() |
|
| 51 | 43 | ->ifTrue(function ($v) use ($configTypeKeys) { |
|
| 52 | 43 | if (!empty($v) && is_array($v)) { |
|
| 53 | 43 | $keys = array_keys($v); |
|
| 54 | 43 | foreach ($configTypeKeys as $configTypeKey) { |
|
| 55 | 5 | if (in_array($configTypeKey, $keys)) { |
|
| 56 | return true; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | 38 | ||
| 61 | 47 | return false; |
|
| 62 | 47 | }) |
|
| 63 | 47 | ->thenInvalid( |
|
| 64 | sprintf( |
||
| 65 | 47 | 'Don\'t use internal config keys %s, replace it by "config" instead.', |
|
| 66 | implode(', ', $configTypeKeys) |
||
| 67 | ) |
||
| 68 | 47 | ) |
|
| 69 | ->end() |
||
| 70 | 47 | # temporarily rename 'config' into '_{TYPE}_config' |
|
| 71 | 47 | ->beforeNormalization() |
|
| 72 | 47 | ->ifTrue(fn ($v) => isset($v['type']) && is_string($v['type'])) |
|
| 73 | 38 | ->then(function ($v) { |
|
| 74 | $key = $this->normalizedConfigTypeKey($v['type']); |
||
| 75 | 38 | ||
| 76 | 38 | if (empty($v[$key])) { |
|
| 77 | $v[$key] = $v['config'] ?? []; |
||
| 78 | 38 | } |
|
| 79 | unset($v['config']); |
||
| 80 | 38 | ||
| 81 | 47 | return $v; |
|
| 82 | 47 | }) |
|
| 83 | 47 | ->end() |
|
| 84 | 47 | # temporarily convert '{MODEL}' into '{TYPE}_{MODEL}' |
|
| 85 | 47 | ->beforeNormalization() |
|
| 86 | 47 | ->ifTrue(fn($v) => isset($v['model']) && is_string($v['model'])) |
|
| 87 | 47 | ->then(function($v) { |
|
| 88 | 47 | $v['model'] = "{$v['type']}_{$v['model']}"; |
|
| 89 | 47 | return $v; |
|
| 90 | 47 | }) |
|
| 91 | 47 | ->end() |
|
| 92 | 47 | ->cannotBeOverwritten() |
|
| 93 | 47 | ->children() |
|
| 94 | 47 | ->scalarNode('class_name') |
|
| 95 | 47 | ->isRequired() |
|
| 96 | 47 | ->validate() |
|
| 97 | 47 | ->ifTrue(fn ($name) => !preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name)) |
|
| 98 | 47 | ->thenInvalid('A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.') |
|
| 99 | 47 | ->end() |
|
| 100 | 47 | ->end() |
|
| 101 | 47 | ->enumNode('type')->values(self::$types)->isRequired()->end() |
|
| 102 | 47 | ->arrayNode(InheritanceProcessor::INHERITS_KEY) |
|
| 103 | 47 | ->prototype('scalar')->info('Types to inherit of.')->end() |
|
| 104 | 47 | ->end() |
|
| 105 | ->booleanNode('decorator')->info('Decorator will not be generated.')->defaultFalse()->end() |
||
| 106 | 47 | ->append(Config\ObjectTypeDefinition::create()->getDefinition()) |
|
| 107 | 47 | ->append(Config\EnumTypeDefinition::create()->getDefinition()) |
|
| 108 | 47 | ->append(Config\InterfaceTypeDefinition::create()->getDefinition()) |
|
| 109 | 37 | ->append(Config\UnionTypeDefinition::create()->getDefinition()) |
|
| 110 | 37 | ->append(Config\InputObjectTypeDefinition::create()->getDefinition()) |
|
| 111 | 37 | ->append(Config\CustomScalarTypeDefinition::create()->getDefinition()) |
|
| 112 | ->variableNode('config')->end() |
||
| 113 | 37 | ->append($this->modelSection()) |
|
| 114 | 47 | ->end() |
|
| 115 | 47 | // rename '_{TYPE}_config' back into 'config' |
|
| 116 | ->validate() |
||
| 117 | 47 | ->ifTrue(fn ($v) => isset($v[$this->normalizedConfigTypeKey($v['type'])])) |
|
| 118 | ->then(function ($v) { |
||
| 119 | 47 | $key = $this->normalizedConfigTypeKey($v['type']); |
|
| 120 | $v['config'] = $v[$key]; |
||
| 121 | unset($v[$key]); |
||
| 122 | 47 | ||
| 123 | return $v; |
||
| 124 | }) |
||
| 125 | ->end() |
||
| 126 | 47 | ||
| 127 | 47 | ->end(); |
|
| 128 | 47 | ||
| 129 | 47 | return $treeBuilder; |
|
| 130 | } |
||
| 167 |