Conditions | 10 |
Paths | 160 |
Total Lines | 70 |
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 |
||
100 | public function createManager($managerName, $connection, $analysis, $managerConfig) |
||
101 | { |
||
102 | $mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']); |
||
103 | |||
104 | $client = ClientBuilder::create(); |
||
105 | $client->setHosts($connection['hosts']); |
||
106 | |||
107 | if ($this->tracer) { |
||
108 | $client->setTracer($this->tracer); |
||
109 | } |
||
110 | |||
111 | if ($this->logger && $managerConfig['logger']['enabled']) { |
||
112 | $client->setLogger($this->logger); |
||
113 | } |
||
114 | |||
115 | $indexSettings = [ |
||
116 | 'index' => $connection['index_name'], |
||
117 | 'body' => array_filter( |
||
118 | [ |
||
119 | 'settings' => array_merge( |
||
120 | $connection['settings'], |
||
121 | [ |
||
122 | 'analysis' => |
||
123 | $this->metadataCollector->getClientAnalysis($managerConfig['mappings'], $analysis), |
||
124 | ] |
||
125 | ), |
||
126 | 'mappings' => $mappings, |
||
127 | ] |
||
128 | ), |
||
129 | ]; |
||
130 | |||
131 | if (class_exists(Versions::class)) { |
||
132 | $elasticSearchVersion = explode('@', Versions::getVersion('ongr/elasticsearch-dsl'))[0]; |
||
133 | if (0 === strpos($elasticSearchVersion, 'v')) { |
||
134 | $elasticSearchVersion = substr($elasticSearchVersion, 1); |
||
135 | } |
||
136 | if (version_compare($elasticSearchVersion, '7.0.0', '>=')) { |
||
137 | $indexSettings['include_type_name'] = true; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | $this->eventDispatcher && |
||
142 | $this->eventDispatcher->dispatch( |
||
143 | Events::PRE_MANAGER_CREATE, |
||
144 | $preCreateEvent = new PreCreateManagerEvent($client, $indexSettings) |
||
145 | ); |
||
146 | |||
147 | $manager = new Manager( |
||
148 | $managerName, |
||
149 | $managerConfig, |
||
150 | $client->build(), |
||
151 | $preCreateEvent->getIndexSettings(), |
||
|
|||
152 | $this->metadataCollector, |
||
153 | $this->converter |
||
154 | ); |
||
155 | |||
156 | if (isset($this->stopwatch)) { |
||
157 | $manager->setStopwatch($this->stopwatch); |
||
158 | } |
||
159 | |||
160 | $manager->setCommitMode($managerConfig['commit_mode']); |
||
161 | $manager->setEventDispatcher($this->eventDispatcher); |
||
162 | $manager->setCommitMode($managerConfig['commit_mode']); |
||
163 | $manager->setBulkCommitSize($managerConfig['bulk_size']); |
||
164 | |||
165 | $this->eventDispatcher && |
||
166 | $this->eventDispatcher->dispatch(Events::POST_MANAGER_CREATE, new PostCreateManagerEvent($manager)); |
||
167 | |||
168 | return $manager; |
||
169 | } |
||
170 | } |
||
171 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: