These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Sylius package. |
||
5 | * |
||
6 | * (c) Paweł Jędrzejewski |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Sylius\Bundle\UserBundle\DependencyInjection; |
||
15 | |||
16 | use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; |
||
17 | use Sylius\Bundle\UserBundle\Controller\UserController; |
||
18 | use Sylius\Component\Resource\Factory\Factory; |
||
19 | use Sylius\Component\User\Model\User; |
||
20 | use Sylius\Component\User\Model\UserInterface; |
||
21 | use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
||
22 | use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
||
23 | use Symfony\Component\Config\Definition\ConfigurationInterface; |
||
24 | |||
25 | final class Configuration implements ConfigurationInterface |
||
26 | { |
||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | public function getConfigTreeBuilder(): TreeBuilder |
||
31 | { |
||
32 | $treeBuilder = new TreeBuilder(); |
||
33 | $rootNode = $treeBuilder->root('sylius_user'); |
||
34 | |||
35 | $rootNode |
||
36 | ->addDefaultsIfNotSet() |
||
37 | ->children() |
||
38 | ->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end() |
||
39 | ->end() |
||
40 | ; |
||
41 | |||
42 | $this->addResourcesSection($rootNode); |
||
43 | |||
44 | return $treeBuilder; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param ArrayNodeDefinition $node |
||
49 | */ |
||
50 | private function addResourcesSection(ArrayNodeDefinition $node): void |
||
51 | { |
||
52 | $node |
||
0 ignored issues
–
show
|
|||
53 | ->children() |
||
54 | ->arrayNode('resources') |
||
55 | ->useAttributeAsKey('name') |
||
56 | ->prototype('array') |
||
57 | ->children() |
||
58 | ->arrayNode('user') |
||
59 | ->addDefaultsIfNotSet() |
||
60 | ->children() |
||
61 | ->scalarNode('templates')->defaultValue('SyliusUserBundle:User')->end() |
||
62 | ->variableNode('options')->end() |
||
63 | ->arrayNode('resetting') |
||
64 | ->addDefaultsIfNotSet() |
||
65 | ->children() |
||
66 | ->arrayNode('token') |
||
67 | ->addDefaultsIfNotSet() |
||
68 | ->children() |
||
69 | ->scalarNode('ttl')->defaultValue('P1D')->end() |
||
70 | ->integerNode('length') |
||
71 | ->defaultValue(16) |
||
72 | ->min(1)->max(40) |
||
73 | ->end() |
||
74 | ->scalarNode('field_name') |
||
75 | ->defaultValue('passwordResetToken') |
||
76 | ->validate() |
||
77 | ->ifTrue(function ($tokenFieldName) { |
||
78 | return !is_string($tokenFieldName); |
||
79 | }) |
||
80 | ->thenInvalid('Invalid resetting token field "%s"') |
||
81 | ->end() |
||
82 | ->end() |
||
83 | ->end() |
||
84 | ->end() |
||
85 | ->arrayNode('pin') |
||
86 | ->addDefaultsIfNotSet() |
||
87 | ->children() |
||
88 | ->integerNode('length') |
||
89 | ->defaultValue(4) |
||
90 | ->min(1)->max(9) |
||
91 | ->end() |
||
92 | ->scalarNode('field_name') |
||
93 | ->defaultValue('passwordResetToken') |
||
94 | ->validate() |
||
95 | ->ifTrue(function ($passwordResetToken) { |
||
96 | return !is_string($passwordResetToken); |
||
97 | }) |
||
98 | ->thenInvalid('Invalid resetting pin field "%s"') |
||
99 | ->end() |
||
100 | ->end() |
||
101 | ->end() |
||
102 | ->end() |
||
103 | ->end() |
||
104 | ->end() |
||
105 | ->arrayNode('verification') |
||
106 | ->addDefaultsIfNotSet() |
||
107 | ->children() |
||
108 | ->arrayNode('token') |
||
109 | ->addDefaultsIfNotSet() |
||
110 | ->children() |
||
111 | ->integerNode('length') |
||
112 | ->defaultValue(16) |
||
113 | ->min(1)->max(40) |
||
114 | ->end() |
||
115 | ->scalarNode('field_name') |
||
116 | ->defaultValue('emailVerificationToken') |
||
117 | ->validate() |
||
118 | ->ifTrue(function ($emailVerificationToken) { |
||
119 | return !is_string($emailVerificationToken); |
||
120 | }) |
||
121 | ->thenInvalid('Invalid verification token field "%s"') |
||
122 | ->end() |
||
123 | ->end() |
||
124 | ->end() |
||
125 | ->end() |
||
126 | ->end() |
||
127 | ->end() |
||
128 | ->arrayNode('classes') |
||
129 | ->addDefaultsIfNotSet() |
||
130 | ->children() |
||
131 | ->scalarNode('model')->defaultValue(User::class)->cannotBeEmpty()->end() |
||
132 | ->scalarNode('interface')->defaultValue(UserInterface::class)->cannotBeEmpty()->end() |
||
133 | ->scalarNode('controller')->defaultValue(UserController::class)->cannotBeEmpty()->end() |
||
134 | ->scalarNode('repository')->cannotBeEmpty()->end() |
||
135 | ->scalarNode('factory')->defaultValue(Factory::class)->end() |
||
136 | ->scalarNode('form')->cannotBeEmpty()->end() |
||
137 | ->end() |
||
138 | ->end() |
||
139 | ->end() |
||
140 | ->end() |
||
141 | ->end() |
||
142 | ->end() |
||
143 | ->end() |
||
144 | ->end() |
||
145 | ; |
||
146 | } |
||
147 | } |
||
148 |
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: