| Total Complexity | 47 |
| Total Lines | 265 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 1 | Features | 0 |
Complex classes like PHP often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PHP, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class PHP extends Abstracts\Engine implements Interfaces\Engine |
||
| 44 | { |
||
| 45 | private $phpCliEscaped; |
||
| 46 | |||
| 47 | private $phpCli; |
||
| 48 | |||
| 49 | private $phpize; |
||
| 50 | |||
| 51 | private $version; |
||
| 52 | |||
| 53 | private $major; |
||
| 54 | |||
| 55 | private $minor; |
||
| 56 | |||
| 57 | private $release; |
||
| 58 | |||
| 59 | private $extra; |
||
| 60 | |||
| 61 | private $compiler; |
||
| 62 | |||
| 63 | private $architecture; |
||
| 64 | |||
| 65 | private $zts; |
||
| 66 | |||
| 67 | private $debug; |
||
| 68 | |||
| 69 | private $iniPath; |
||
| 70 | |||
| 71 | private $extensionDir; |
||
| 72 | |||
| 73 | private $hasSdk; |
||
| 74 | |||
| 75 | public function __construct($phpCli = PHP_BINARY) |
||
| 76 | { |
||
| 77 | if (!(is_file($phpCli) && is_executable($phpCli))) { |
||
| 78 | throw new Exception("Invalid php executable: {$phpCli}"); |
||
| 79 | } |
||
| 80 | $this->phpCliEscaped = escapeshellcmd($phpCli); |
||
| 81 | $this->phpCli = $phpCli; |
||
| 82 | $this->getFromConstants(); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function getName() |
||
| 86 | { |
||
| 87 | return 'php'; |
||
| 88 | } |
||
| 89 | |||
| 90 | public function hasSdk() |
||
| 91 | { |
||
| 92 | if (isset($this->hasSdk)) { |
||
| 93 | return $this->hasSdk; |
||
| 94 | } |
||
| 95 | $cliDir = dirname($this->phpCli); |
||
| 96 | $res = glob($cliDir . DIRECTORY_SEPARATOR . 'phpize*'); |
||
| 97 | if (!$res) { |
||
|
|
|||
| 98 | $this->hasSdk = false; |
||
| 99 | } |
||
| 100 | $this->phpize = $res[0]; |
||
| 101 | |||
| 102 | return $this->hasSdk = false; |
||
| 103 | } |
||
| 104 | |||
| 105 | public function getArchitecture() |
||
| 106 | { |
||
| 107 | return $this->architecture; |
||
| 108 | } |
||
| 109 | |||
| 110 | public function getCompiler() |
||
| 111 | { |
||
| 112 | return $this->compiler; |
||
| 113 | } |
||
| 114 | |||
| 115 | public function getPath() |
||
| 116 | { |
||
| 117 | return $this->phpCli; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function getMajorVersion() |
||
| 121 | { |
||
| 122 | return $this->major; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function getMinorVersion() |
||
| 126 | { |
||
| 127 | return $this->minor; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getReleaseVersion() |
||
| 131 | { |
||
| 132 | return $this->release; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function getVersion() |
||
| 136 | { |
||
| 137 | return $this->version; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function getZts() |
||
| 141 | { |
||
| 142 | return $this->zts; |
||
| 143 | } |
||
| 144 | |||
| 145 | public function getExtensionDir() |
||
| 148 | } |
||
| 149 | |||
| 150 | public function getIniPath() |
||
| 153 | } |
||
| 154 | |||
| 155 | protected function getFullPathExtDir($dir) |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | protected function getExtensionDirFromPhpInfo($info) |
||
| 173 | { |
||
| 174 | $extensionDir = ''; |
||
| 175 | |||
| 176 | foreach ($info as $s) { |
||
| 177 | $pos_ext_dir = strpos($s, 'extension_dir'); |
||
| 178 | if ($pos_ext_dir !== false && substr($s, $pos_ext_dir - 1, 1) != '.') { |
||
| 179 | [, $extensionDir] = explode('=>', $s); |
||
| 180 | break; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | $extensionDir = trim($extensionDir); |
||
| 185 | if ($extensionDir == '') { |
||
| 186 | throw new Exception('Cannot detect PHP extension directory'); |
||
| 187 | } |
||
| 188 | return $this->getFullPathExtDir($extensionDir); |
||
| 189 | } |
||
| 190 | |||
| 191 | protected function getArchFromPhpInfo($info) |
||
| 192 | { |
||
| 193 | $arch = ''; |
||
| 194 | |||
| 195 | foreach ($info as $s) { |
||
| 196 | if (strpos($s, 'Architecture') !== false) { |
||
| 197 | [, $arch] = explode('=>', $s); |
||
| 198 | break; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | $arch = trim($arch); |
||
| 203 | if ($arch == '') { |
||
| 204 | throw new Exception('Cannot detect PHP build architecture'); |
||
| 205 | } |
||
| 206 | |||
| 207 | return $arch; |
||
| 208 | } |
||
| 209 | |||
| 210 | protected function getIniPathFromPhpInfo($info) |
||
| 211 | { |
||
| 212 | $iniPath = ''; |
||
| 213 | |||
| 214 | foreach ($info as $s) { |
||
| 215 | if (strpos($s, 'Loaded Configuration File') !== false) { |
||
| 216 | [, $iniPath] = explode('=>', $s); |
||
| 217 | if ($iniPath === '(None)') { |
||
| 218 | $iniPath = ''; |
||
| 219 | } |
||
| 220 | |||
| 221 | break; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | $iniPath = trim($iniPath); |
||
| 226 | if ($iniPath == '') { |
||
| 227 | throw new Exception('Cannot detect php.ini directory'); |
||
| 228 | } |
||
| 229 | |||
| 230 | return $iniPath; |
||
| 231 | } |
||
| 232 | |||
| 233 | protected function getCompilerFromPhpInfo($info) |
||
| 259 | } |
||
| 260 | |||
| 261 | private function getFromConstants() |
||
| 262 | { |
||
| 263 | $script = 'echo PHP_VERSION . \"\n\"; ' |
||
| 264 | . 'echo PHP_MAJOR_VERSION . \"\n\"; ' |
||
| 265 | . 'echo PHP_MINOR_VERSION . \"\n\"; ' |
||
| 266 | . 'echo PHP_RELEASE_VERSION . \"\n\"; ' |
||
| 267 | . 'echo PHP_EXTRA_VERSION . \"\n\"; ' |
||
| 268 | . 'echo PHP_ZTS . \"\n\"; ' |
||
| 269 | . 'echo PHP_DEBUG . \"\n\"; '; |
||
| 270 | |||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | private function getFromPhpInfo() |
||
| 312 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.