| Total Complexity | 70 |
| Total Lines | 491 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like GitFilesCommand 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 GitFilesCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class GitFilesCommand extends AbstractGitCommand |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * List files in directory with git log. |
||
| 37 | * |
||
| 38 | * @param string $dir |
||
| 39 | * |
||
| 40 | * @param string $branch |
||
| 41 | * @param bool $gitFilesOnly |
||
| 42 | * |
||
| 43 | * @return array remoteFileInfo/fileInfo |
||
| 44 | * @throws InvalidDirectoryException |
||
| 45 | * @throws Exception |
||
| 46 | */ |
||
| 47 | public function listFiles($dir, $branch = 'master', $gitFilesOnly = false): array |
||
| 48 | { |
||
| 49 | $files = array(); |
||
| 50 | $fileList = $this->getFilesInDirectory($dir); |
||
| 51 | |||
| 52 | foreach ($fileList as $fileInfo) { |
||
| 53 | $fileLastLog = $this->getLog(1, $branch, $fileInfo->getGitPath()); |
||
| 54 | |||
| 55 | if (count($fileLastLog) > 0) { |
||
| 56 | $fileInfo->setGitLog($fileLastLog[0]); |
||
| 57 | $files[] = $fileInfo; |
||
| 58 | } elseif ($gitFilesOnly === false) { |
||
| 59 | $files[] = $fileInfo; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | return $files; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param $path |
||
| 68 | * @param string $branch |
||
| 69 | * |
||
| 70 | * @return SPLFileInfo|RemoteFileInfo |
||
| 71 | * @throws InvalidDirectoryException |
||
| 72 | * @throws Exception |
||
| 73 | */ |
||
| 74 | public function getFile($path, $branch = 'master') |
||
| 75 | { |
||
| 76 | $fileInfo = $this->getFileInfo($path); |
||
| 77 | $fileLastLog = $this->getLog(1, $branch, $fileInfo->getGitPath()); |
||
| 78 | if (count($fileLastLog) > 0) { |
||
| 79 | $fileInfo->setGitLog($fileLastLog[0]); |
||
| 80 | } |
||
| 81 | |||
| 82 | return $fileInfo; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param $path |
||
| 87 | * |
||
| 88 | * @return SplFileInfo|RemoteFileInfo |
||
| 89 | * @throws InvalidDirectoryException |
||
| 90 | */ |
||
| 91 | protected function getFileInfo($path) |
||
| 92 | { |
||
| 93 | |||
| 94 | if ($this->validPathStr($path) === false) { |
||
| 95 | throw new InvalidDirectoryException('Directory path is not valid. Possible security issue.'); |
||
| 96 | } |
||
| 97 | |||
| 98 | $basePath = $this->addEndingSlash($this->command->getGitEnvironment()->getPath()); |
||
| 99 | |||
| 100 | if ($this->command->getGitEnvironment()->getSsh() === true) { |
||
| 101 | //Remote Directory Listing |
||
| 102 | |||
| 103 | $fileData = $this->command->getSftpProcess()->getFileStats($basePath . $path); |
||
| 104 | |||
| 105 | $fileData['filename'] = basename($path); |
||
| 106 | $fileData['fullPath'] = $basePath . $path; |
||
| 107 | $fileData['gitPath'] = $path; |
||
| 108 | |||
| 109 | return new RemoteFileInfo($fileData); |
||
| 110 | } |
||
| 111 | |||
| 112 | $splFileInfo = new SPLFileInfo($basePath . $path); |
||
| 113 | $splFileInfo->setInfoClass(FileInfo::class); |
||
| 114 | |||
| 115 | $newFileInfo = $splFileInfo->getFileInfo(); |
||
| 116 | if (!$newFileInfo instanceof FileInfo) { |
||
| 117 | throw new RuntimeException(sprintf('File info must be a instance of "%s"', FileInfo::class)); |
||
| 118 | } |
||
| 119 | |||
| 120 | $newFileInfo->setGitPath($basePath . $path); |
||
| 121 | |||
| 122 | return $newFileInfo; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Adds Ending slash where needed for unix and windows paths. |
||
| 127 | * |
||
| 128 | * @param string $path |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | protected function addEndingSlash($path): string |
||
| 133 | { |
||
| 134 | $slash_type = (strpos($path, '\\') === 0) ? 'win' : 'unix'; |
||
| 135 | $last_char = $path[strlen($path) - 1]; |
||
| 136 | if ($last_char !== '/' && $last_char !== '\\') { |
||
| 137 | // no slash: |
||
| 138 | $path .= ($slash_type === 'win') ? '\\' : '/'; |
||
| 139 | } |
||
| 140 | |||
| 141 | return $path; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Sort files by directory then name. |
||
| 146 | * |
||
| 147 | * @param array $fileArray |
||
| 148 | */ |
||
| 149 | protected function sortFilesByDirectoryThenName(array &$fileArray): void |
||
| 150 | { |
||
| 151 | usort($fileArray, static function (FileInfoInterface $a, FileInfoInterface $b) { |
||
| 152 | if ($a->isDir()) { |
||
| 153 | if ($b->isDir()) { |
||
| 154 | return strnatcasecmp($a->getFilename(), $b->getFilename()); |
||
| 155 | } |
||
| 156 | |||
| 157 | return -1; |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($b->isDir()) { |
||
| 161 | return 1; |
||
| 162 | } |
||
| 163 | |||
| 164 | return strnatcasecmp($a->getFilename(), $b->getFilename()); |
||
| 165 | }); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get files in directory locally and remotely. |
||
| 170 | * |
||
| 171 | * @param string $dir full path to directory |
||
| 172 | * |
||
| 173 | * @return array of files |
||
| 174 | * @throws InvalidDirectoryException |
||
| 175 | */ |
||
| 176 | public function getFilesInDirectory($dir): array |
||
| 177 | { |
||
| 178 | if ($this->validPathStr($dir) === false) { |
||
| 179 | throw new InvalidDirectoryException('Directory path is not valid. Possible security issue.'); |
||
| 180 | } |
||
| 181 | |||
| 182 | $files = array(); |
||
| 183 | $basePath = $this->addEndingSlash($this->command->getGitEnvironment()->getPath()); |
||
| 184 | $relativePath = $dir; |
||
| 185 | if ($relativePath) { |
||
| 186 | $relativePath = $this->addEndingSlash($relativePath); |
||
| 187 | } |
||
| 188 | |||
| 189 | if ($this->command->getGitEnvironment()->getSsh() === true) { |
||
| 190 | //Remote Directory Listing |
||
| 191 | $directoryList = $this->command->getSftpProcess()->getDirectoryList($basePath . $relativePath); |
||
| 192 | |||
| 193 | foreach ($directoryList as $filename => $fileData) { |
||
| 194 | if ($filename !== '.' && $filename !== '..' && $filename !== '.git') { |
||
| 195 | $fileData['fullPath'] = $basePath . rtrim($relativePath, '/') . '/' . $filename; |
||
| 196 | $fileData['gitPath'] = $relativePath . $filename; |
||
| 197 | |||
| 198 | $remoteFileInfo = new RemoteFileInfo($fileData); |
||
| 199 | |||
| 200 | $files[] = $remoteFileInfo; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } else { |
||
| 204 | //Local Directory Listing |
||
| 205 | $directoryIterator = new DirectoryIterator($basePath . $dir); |
||
| 206 | $directoryIterator->setInfoClass(FileInfo::class); |
||
| 207 | |||
| 208 | foreach ($directoryIterator as $fileInfo) { |
||
| 209 | if (!$fileInfo->isDot() && $fileInfo->getFilename() !== '.git') { |
||
| 210 | $newFileInfo = $fileInfo->getFileInfo(); |
||
| 211 | if (!$newFileInfo instanceof FileInfo) { |
||
| 212 | throw new RuntimeException(sprintf('File info must be a instance of "%s"', FileInfo::class)); |
||
| 213 | } |
||
| 214 | $newFileInfo->setGitPath($relativePath . $fileInfo->getFilename()); |
||
| 215 | |||
| 216 | $files[] = $newFileInfo; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | $this->sortFilesByDirectoryThenName($files); |
||
| 222 | |||
| 223 | return $files; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Read Git File |
||
| 228 | * |
||
| 229 | * @param FileInfoInterface $file |
||
| 230 | * |
||
| 231 | * @return string file contents |
||
| 232 | */ |
||
| 233 | public function readFile(FileInfoInterface $file): string |
||
| 234 | { |
||
| 235 | if ($this->command->getGitEnvironment()->getSsh() === true) { |
||
| 236 | return $this->command->getSftpProcess()->getFileContents($file->getFullPath()); |
||
| 237 | } |
||
| 238 | |||
| 239 | return file_get_contents($file->getFullPath()); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Checks for malicious file paths. |
||
| 244 | * |
||
| 245 | * Returns TRUE if no '//', '..', '\' or control characters are found in the $theFile. |
||
| 246 | * This should make sure that the path is not pointing 'backwards' and further doesn't contain double/back slashes. |
||
| 247 | * So it's compatible with the UNIX style path strings valid for TYPO3 internally. |
||
| 248 | * |
||
| 249 | * @param string $theFile File path to evaluate |
||
| 250 | * |
||
| 251 | * @return bool TRUE, $theFile is allowed path string, FALSE otherwise |
||
| 252 | * |
||
| 253 | * @see http://php.net/manual/en/security.filesystem.nullbytes.php |
||
| 254 | * |
||
| 255 | * @todo Possible improvement: Should it rawurldecode the string first to check if any of these characters is |
||
| 256 | * encoded? |
||
| 257 | */ |
||
| 258 | public function validPathStr($theFile): bool |
||
| 259 | { |
||
| 260 | return strpos($theFile, '//') === false |
||
| 261 | && strpos($theFile, '\\') === false |
||
| 262 | && strpos($theFile, '/') !== 0 |
||
| 263 | && strpos($theFile, '\\') !== 0 |
||
| 264 | && preg_match('#(?:^\\.\\.|/\\.\\./|.git|[[:cntrl:]])#u', $theFile) === 0; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param $filePaths |
||
| 269 | * @param string $mode |
||
| 270 | * |
||
| 271 | * @throws RunGitCommandException |
||
| 272 | */ |
||
| 273 | public function setFilesPermissions($filePaths, $mode = '0775'): void |
||
| 274 | { |
||
| 275 | if (false === $this->command->getGitEnvironment()->getSsh()) { |
||
| 276 | return; |
||
| 277 | } |
||
| 278 | //Remote Directory Listing |
||
| 279 | foreach ($filePaths as $filepath) { |
||
| 280 | $this->command->runCommand(sprintf('chmod -R %s %s', $mode, escapeshellarg($filepath))); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param $filePaths |
||
| 286 | * @param string $user |
||
| 287 | * @param string $group |
||
| 288 | * |
||
| 289 | * @throws RunGitCommandException |
||
| 290 | */ |
||
| 291 | public function setFilesOwnerAndGroup($filePaths, $user = 'www-data', $group = 'fr_user'): void |
||
| 292 | { |
||
| 293 | if (false === $this->command->getGitEnvironment()->getSsh()) { |
||
| 294 | return; |
||
| 295 | } |
||
| 296 | |||
| 297 | //Remote Directory Listing |
||
| 298 | if (trim($user)) { |
||
| 299 | foreach ($filePaths as $filepath) { |
||
| 300 | $this->command->runCommand(sprintf('chown -R %s %s', $user, escapeshellarg($filepath))); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | if (trim($group)) { |
||
| 304 | foreach ($filePaths as $filepath) { |
||
| 305 | $this->command->runCommand(sprintf('chgrp -R %s %s', $group, escapeshellarg($filepath))); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Gets the git log (history) of commits |
||
| 312 | * Currenly limits to the last 20 commits. |
||
| 313 | * |
||
| 314 | * @param int $count |
||
| 315 | * @param string $branch |
||
| 316 | * @param bool $fileName |
||
| 317 | * |
||
| 318 | * @return GitLog|array |
||
| 319 | * @throws Exception |
||
| 320 | */ |
||
| 321 | public function getLog($count = 20, $branch = 'master', $fileName = false) |
||
| 322 | { |
||
| 323 | $gitLogCommand = $this->command->command('log'); |
||
| 324 | |||
| 325 | $gitLogCommand->setLogCount($count); |
||
| 326 | $gitLogCommand->setBranch($branch); |
||
| 327 | $gitLogCommand->setPath($fileName); |
||
| 328 | |||
| 329 | return $gitLogCommand->execute()->getResults(); |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Check if a file is ignored |
||
| 334 | * EXIT STATUS. |
||
| 335 | * 0 |
||
| 336 | * One or more of the provided paths is ignored. |
||
| 337 | * |
||
| 338 | * 1 |
||
| 339 | * None of the provided paths are ignored. |
||
| 340 | * |
||
| 341 | * 128 |
||
| 342 | * A fatal error was encountered. |
||
| 343 | * |
||
| 344 | * @param string $filePath |
||
| 345 | * |
||
| 346 | * @return bool |
||
| 347 | * @throws RuntimeException |
||
| 348 | * @throws RunGitCommandException |
||
| 349 | */ |
||
| 350 | public function isFileIgnored($filePath): bool |
||
| 351 | { |
||
| 352 | try { |
||
| 353 | $response = $this->command->runCommand(sprintf('git check-ignore %s', escapeshellarg($filePath))); |
||
| 354 | } catch (RunGitCommandException $e) { |
||
| 355 | if ($this->command->getLastExitStatus() === 128) { |
||
| 356 | throw $e; |
||
| 357 | } |
||
| 358 | |||
| 359 | $response = $this->command->getLastExitStatus() !== 1; |
||
| 360 | } |
||
| 361 | |||
| 362 | return $response ? true : false; |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Check if a file is been tracked by git. |
||
| 367 | * |
||
| 368 | * @param string $filePath |
||
| 369 | * |
||
| 370 | * @return bool |
||
| 371 | * @throws RunGitCommandException |
||
| 372 | * @throws RuntimeException |
||
| 373 | */ |
||
| 374 | public function isFileTracked($filePath): bool |
||
| 375 | { |
||
| 376 | $response = $this->command->runCommand(sprintf('git ls-files %s', escapeshellarg($filePath))); |
||
| 377 | |||
| 378 | return trim($response) !== ''; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Ignore a file by adding to gitignore |
||
| 383 | * |
||
| 384 | * @param string $filePath |
||
| 385 | * |
||
| 386 | * @return string |
||
| 387 | * @throws RunGitCommandException |
||
| 388 | * @throws RuntimeException |
||
| 389 | * @throws FileStatusException |
||
| 390 | * @throws InvalidFilePathException |
||
| 391 | */ |
||
| 392 | public function ignoreFile($filePath): string |
||
| 393 | { |
||
| 394 | if (false === $this->fileExists($filePath)) { |
||
| 395 | throw new InvalidFilePathException('File path was not valid. Please check that the file exists.'); |
||
| 396 | } |
||
| 397 | |||
| 398 | if ($this->isFileTracked($filePath)) { |
||
| 399 | throw new FileStatusException('File path is been tracked. Please un-track file first'); |
||
| 400 | } |
||
| 401 | |||
| 402 | if ($this->isFileIgnored($filePath) === false) { |
||
| 403 | return $this->addToGitIgnore($filePath); |
||
| 404 | } |
||
| 405 | |||
| 406 | throw new FileStatusException('File path is already ignored'); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Removes file path from git index |
||
| 411 | * |
||
| 412 | * Update your .gitignore file – for instance, add a folder you don't want to track to .gitignore . |
||
| 413 | * git rm -r --cached . – Remove all tracked files, including wanted and unwanted. Your code will be safe as |
||
| 414 | * long as you have saved locally. |
||
| 415 | * git add . – All files will be added back in, except those in .gitignore . |
||
| 416 | * |
||
| 417 | * @param string $filePath |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | * @throws RunGitCommandException |
||
| 421 | * @throws InvalidArgumentException |
||
| 422 | * @throws RuntimeException |
||
| 423 | * @throws FileStatusException |
||
| 424 | * @throws InvalidFilePathException |
||
| 425 | */ |
||
| 426 | public function unTrackFile($filePath): string |
||
| 427 | { |
||
| 428 | if (false === $this->fileExists($filePath)) { |
||
| 429 | throw new InvalidFilePathException('File path was not valid. Please check that the file exists.'); |
||
| 430 | } |
||
| 431 | |||
| 432 | if (false === $this->isFileTracked($filePath)) { |
||
| 433 | throw new FileStatusException('File path is not been tracked'); |
||
| 434 | } |
||
| 435 | |||
| 436 | $statusCount = $this->command->command('commit')->countStatus(); |
||
| 437 | |||
| 438 | if ($statusCount > 0) { |
||
| 439 | throw new FileStatusException('Please commit all files first'); |
||
| 440 | } |
||
| 441 | |||
| 442 | $response = $this->command->runCommand(sprintf('git rm --cached %s', escapeshellarg($filePath))); |
||
| 443 | $response .= "\n Please commit to complete the removal of this file from git index"; |
||
| 444 | |||
| 445 | return $response; |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Checks if a file exists. |
||
| 450 | * |
||
| 451 | * @param string $filePath FilePath excluding base path |
||
| 452 | * |
||
| 453 | * @return bool |
||
| 454 | */ |
||
| 455 | public function fileExists($filePath): bool |
||
| 456 | { |
||
| 457 | $basePath = trim($this->addEndingSlash($this->command->getGitEnvironment()->getPath())); |
||
| 458 | if ($this->command->getGitEnvironment()->getSsh() === true) { |
||
| 459 | return $this->command->getSftpProcess()->fileExists($basePath . $filePath); |
||
| 460 | } |
||
| 461 | |||
| 462 | return file_exists($basePath . $filePath); |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @param $filePath |
||
| 467 | * |
||
| 468 | * @return bool |
||
| 469 | */ |
||
| 470 | public function filePathIsIgnored($filePath): bool |
||
| 471 | { |
||
| 472 | $ignoreFiles = $this->getGitIgnoreFile(); |
||
| 473 | |||
| 474 | foreach ($ignoreFiles as $ignoreFilePath) { |
||
| 475 | if ($ignoreFilePath === '/' . $filePath) { |
||
| 476 | return true; |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | return false; |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Gets the contents of gitignore file and |
||
| 485 | * splits on newline and returns it as an array. |
||
| 486 | * |
||
| 487 | * @return array |
||
| 488 | */ |
||
| 489 | public function getGitIgnoreFile(): array |
||
| 490 | { |
||
| 491 | $fileData['fullPath'] = '.gitignore'; |
||
| 492 | $fileData['gitPath'] = '.gitignore'; |
||
| 493 | |||
| 494 | $remoteFileInfo = new RemoteFileInfo($fileData); |
||
| 495 | |||
| 496 | $contents = $this->readFile($remoteFileInfo); |
||
| 497 | |||
| 498 | return $this->splitOnNewLine($contents); |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param $filePath |
||
| 503 | * |
||
| 504 | * @return string |
||
| 505 | * @throws RunGitCommandException |
||
| 506 | */ |
||
| 507 | public function addToGitIgnore($filePath): string |
||
| 524 | } |
||
| 525 | } |
||
| 526 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.