Conditions | 1 |
Paths | 1 |
Total Lines | 110 |
Lines | 0 |
Ratio | 0 % |
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 |
||
41 | public function getConfigTreeBuilder() |
||
42 | { |
||
43 | $treeBuilder = new TreeBuilder('swp_multi_tenancy'); |
||
44 | $treeBuilder->getRootNode() |
||
|
|||
45 | ->children() |
||
46 | ->booleanNode('use_orm_listeners') |
||
47 | ->defaultFalse() |
||
48 | ->info('Listeners which make sure that each entity is tenant aware (they append tenant code).') |
||
49 | ->end() |
||
50 | ->arrayNode('persistence') |
||
51 | ->validate() |
||
52 | ->ifTrue(function ($v) { |
||
53 | return count(array_filter($v, function ($persistence) { |
||
54 | return $persistence['enabled']; |
||
55 | })) > 1; |
||
56 | }) |
||
57 | ->thenInvalid('Only one persistence layer can be enabled at the same time.') |
||
58 | ->end() |
||
59 | ->addDefaultsIfNotSet() |
||
60 | ->children() |
||
61 | ->arrayNode('phpcr') |
||
62 | ->addDefaultsIfNotSet() |
||
63 | ->canBeEnabled() |
||
64 | ->children() |
||
65 | ->scalarNode('basepath')->defaultValue('/swp')->end() |
||
66 | ->arrayNode('route_basepaths') |
||
67 | ->prototype('scalar')->end() |
||
68 | ->defaultValue(['routes']) |
||
69 | ->info('Route base paths names') |
||
70 | ->end() |
||
71 | ->scalarNode('content_basepath') |
||
72 | ->defaultValue('content') |
||
73 | ->info('Content base path name') |
||
74 | ->end() |
||
75 | ->scalarNode('menu_basepath') |
||
76 | ->defaultValue('menu') |
||
77 | ->info('Menu base path name') |
||
78 | ->end() |
||
79 | ->scalarNode('media_basepath') |
||
80 | ->defaultValue('media') |
||
81 | ->info('Media base path name') |
||
82 | ->end() |
||
83 | ->scalarNode('tenant_aware_router_class') |
||
84 | ->defaultValue(TenantAwareRouter::class) |
||
85 | ->info('Tenant aware router FQCN') |
||
86 | ->end() |
||
87 | ->arrayNode('classes') |
||
88 | ->addDefaultsIfNotSet() |
||
89 | ->children() |
||
90 | ->arrayNode('tenant') |
||
91 | ->addDefaultsIfNotSet() |
||
92 | ->children() |
||
93 | ->scalarNode('model')->cannotBeEmpty()->defaultValue(Tenant::class)->end() |
||
94 | ->scalarNode('interface')->cannotBeEmpty()->defaultValue(TenantInterface::class)->end() |
||
95 | ->scalarNode('repository')->defaultValue(PHPCRTenantRepository::class)->end() |
||
96 | ->scalarNode('factory')->defaultValue(TenantFactory::class)->end() |
||
97 | ->scalarNode('object_manager_name')->defaultValue(null)->end() |
||
98 | ->end() |
||
99 | ->end() |
||
100 | ->arrayNode('organization') |
||
101 | ->addDefaultsIfNotSet() |
||
102 | ->children() |
||
103 | ->scalarNode('model')->cannotBeEmpty()->defaultValue(Organization::class)->end() |
||
104 | ->scalarNode('interface')->cannotBeEmpty()->defaultValue(OrganizationInterface::class)->end() |
||
105 | ->scalarNode('repository')->defaultValue(PHPCROrganizationRepository::class)->end() |
||
106 | ->scalarNode('factory')->defaultValue(OrganizationFactory::class)->end() |
||
107 | ->scalarNode('object_manager_name')->defaultValue(null)->end() |
||
108 | ->end() |
||
109 | ->end() |
||
110 | ->end() |
||
111 | ->end() |
||
112 | ->end() |
||
113 | ->end() // phpcr |
||
114 | ->arrayNode('orm') |
||
115 | ->addDefaultsIfNotSet() |
||
116 | ->canBeEnabled() |
||
117 | ->children() |
||
118 | ->arrayNode('classes') |
||
119 | ->addDefaultsIfNotSet() |
||
120 | ->children() |
||
121 | ->arrayNode('tenant') |
||
122 | ->addDefaultsIfNotSet() |
||
123 | ->children() |
||
124 | ->scalarNode('model')->cannotBeEmpty()->defaultValue(Tenant::class)->end() |
||
125 | ->scalarNode('interface')->cannotBeEmpty()->defaultValue(TenantInterface::class)->end() |
||
126 | ->scalarNode('repository')->defaultValue(TenantRepository::class)->end() |
||
127 | ->scalarNode('factory')->defaultValue(TenantFactory::class)->end() |
||
128 | ->scalarNode('object_manager_name')->defaultValue(null)->end() |
||
129 | ->end() |
||
130 | ->end() |
||
131 | ->arrayNode('organization') |
||
132 | ->addDefaultsIfNotSet() |
||
133 | ->children() |
||
134 | ->scalarNode('model')->cannotBeEmpty()->defaultValue(Organization::class)->end() |
||
135 | ->scalarNode('interface')->cannotBeEmpty()->defaultValue(OrganizationInterface::class)->end() |
||
136 | ->scalarNode('repository')->defaultValue(OrganizationRepository::class)->end() |
||
137 | ->scalarNode('factory')->defaultValue(OrganizationFactory::class)->end() |
||
138 | ->scalarNode('object_manager_name')->defaultValue(null)->end() |
||
139 | ->end() |
||
140 | ->end() |
||
141 | ->end() |
||
142 | ->end() |
||
143 | ->end() |
||
144 | ->end() // orm |
||
145 | ->end() |
||
146 | ->end() |
||
147 | ->end(); |
||
148 | |||
149 | return $treeBuilder; |
||
150 | } |
||
151 | } |
||
152 |
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: