We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php |
||
11 | class TypesConfiguration implements ConfigurationInterface |
||
12 | { |
||
13 | private static $types = [ |
||
14 | 'object', |
||
15 | 'enum', |
||
16 | 'interface', |
||
17 | 'union', |
||
18 | 'input-object', |
||
19 | 'custom-scalar', |
||
20 | ]; |
||
21 | |||
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 | ||
119 | private function addBeforeNormalization(ArrayNodeDefinition $node) |
||
120 | { |
||
121 | 37 | $node |
|
122 | // process beforeNormalization (should be execute after relay normalization) |
||
123 | ->beforeNormalization() |
||
124 | ->ifTrue(function ($types) { |
||
125 | 37 | return \is_array($types); |
|
126 | }) |
||
127 | 37 | ->then(function ($types) { |
|
128 | 37 | return Config\Processor::process($types, Config\Processor::BEFORE_NORMALIZATION); |
|
129 | }) |
||
130 | 37 | ->end() |
|
131 | 37 | ; |
|
132 | 37 | } |
|
133 | |||
134 | 37 | private function normalizedConfigTypeKey($type) |
|
138 | } |
||
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: