| 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  | 
            ||
| 101 | public function createManager($managerName, $connection, $analysis, $managerConfig)  | 
            ||
| 102 |     { | 
            ||
| 103 | $mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']);  | 
            ||
| 104 | |||
| 105 | $client = ClientBuilder::create();  | 
            ||
| 106 | $client->setHosts($connection['hosts']);  | 
            ||
| 107 | |||
| 108 |         if ($this->tracer) { | 
            ||
| 109 | $client->setTracer($this->tracer);  | 
            ||
| 110 | }  | 
            ||
| 111 | |||
| 112 |         if ($this->logger && $managerConfig['logger']['enabled']) { | 
            ||
| 113 | $client->setLogger($this->logger);  | 
            ||
| 114 | }  | 
            ||
| 115 | |||
| 116 | $indexSettings = [  | 
            ||
| 117 | 'index' => $connection['index_name'],  | 
            ||
| 118 | 'body' => array_filter(  | 
            ||
| 119 | [  | 
            ||
| 120 | 'settings' => array_merge(  | 
            ||
| 121 | $connection['settings'],  | 
            ||
| 122 | [  | 
            ||
| 123 | 'analysis' =>  | 
            ||
| 124 | $this->metadataCollector->getClientAnalysis($managerConfig['mappings'], $analysis),  | 
            ||
| 125 | ]  | 
            ||
| 126 | ),  | 
            ||
| 127 | 'mappings' => $mappings,  | 
            ||
| 128 | ]  | 
            ||
| 129 | ),  | 
            ||
| 130 | ];  | 
            ||
| 131 | |||
| 132 |         if (class_exists(Versions::class)) { | 
            ||
| 133 |             $elasticSearchVersion = explode('@', Versions::getVersion('elasticsearch/elasticsearch'))[0]; | 
            ||
| 134 |             if (0 === strpos($elasticSearchVersion, 'v')) { | 
            ||
| 135 | $elasticSearchVersion = substr($elasticSearchVersion, 1);  | 
            ||
| 136 | }  | 
            ||
| 137 |             if (version_compare($elasticSearchVersion, '7.0.0', '>=')) { | 
            ||
| 138 | $indexSettings['include_type_name'] = true;  | 
            ||
| 139 | }  | 
            ||
| 140 | }  | 
            ||
| 141 | |||
| 142 | $this->eventDispatcher &&  | 
            ||
| 143 | $this->dispatch(  | 
            ||
| 144 | Events::PRE_MANAGER_CREATE,  | 
            ||
| 145 | $preCreateEvent = new PreCreateManagerEvent($client, $indexSettings)  | 
            ||
| 146 | );  | 
            ||
| 147 | |||
| 148 | $manager = new Manager(  | 
            ||
| 149 | $managerName,  | 
            ||
| 150 | $managerConfig,  | 
            ||
| 151 | $client->build(),  | 
            ||
| 152 | $preCreateEvent->getIndexSettings(),  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 153 | $this->metadataCollector,  | 
            ||
| 154 | $this->converter  | 
            ||
| 155 | );  | 
            ||
| 156 | |||
| 157 |         if (isset($this->stopwatch)) { | 
            ||
| 158 | $manager->setStopwatch($this->stopwatch);  | 
            ||
| 159 | }  | 
            ||
| 160 | |||
| 161 | $manager->setCommitMode($managerConfig['commit_mode']);  | 
            ||
| 162 | $manager->setEventDispatcher($this->eventDispatcher);  | 
            ||
| 163 | $manager->setCommitMode($managerConfig['commit_mode']);  | 
            ||
| 164 | $manager->setBulkCommitSize($managerConfig['bulk_size']);  | 
            ||
| 165 | |||
| 166 | $this->eventDispatcher &&  | 
            ||
| 167 | $this->dispatch(Events::POST_MANAGER_CREATE, new PostCreateManagerEvent($manager));  | 
            ||
| 168 | |||
| 169 | return $manager;  | 
            ||
| 170 | }  | 
            ||
| 171 | |||
| 181 | 
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: