Completed
Push — master ( 119a25...af28eb )
by Shcherbak
04:15
created

StandardFileFinder::findFiles()   C

Complexity

Conditions 9
Paths 13

Size

Total Lines 61
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 61
ccs 0
cts 44
cp 0
rs 6.7603
cc 9
eloc 35
nc 13
nop 1
crap 90

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
  namespace Funivan\Cs\Fs\FileFinder;
4
5
  use Funivan\Cs\Fs\File;
6
  use Funivan\Cs\Fs\FilesCollection;
7
  use Symfony\Component\Finder\Finder;
8
  use Symfony\Component\Finder\SplFileInfo;
9
  use Symfony\Component\Process\Process;
10
11
  /**
12
   *
13
   */
14
  class StandardFileFinder implements FileFinderInterface {
15
16
    /**
17
     * @param FinderParameters $finderParameters
18
     * @return FilesCollection
19
     * @internal param FinderParameters $params
20
     */
21
    public function findFiles(FinderParameters $finderParameters) {
22
      $filesCollection = new FilesCollection();
23
24
      if ($finderParameters->get('dir')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $finderParameters->get('dir') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
25
        $dir = $finderParameters->get('dir');
26
27
        $files = (new Finder())->in($dir)->files();
28
29
        /** @var SplFileInfo $file */
30
        foreach ($files as $file) {
31
          $filesCollection->add(new File($file->getRealPath(), File::STATUS_UNKNOWN));
32
        }
33
34
        return $filesCollection;
35
      }
36
37
      $baseDir = $finderParameters->get('baseDir');
38
      if (empty($baseDir)) {
39
        throw new \RuntimeException('Can not find files. Invalid baseDir finder parameter.');
40
      }
41
      $baseDir = rtrim($baseDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
42
      if (!empty($commitHash)) {
0 ignored issues
show
Bug introduced by
The variable $commitHash seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
43
        $process = new Process('git diff-tree --no-commit-id --name-status  -r ' . $commitHash);
44
      } else {
45
        $process = new Process('git diff --name-status ; git diff --cached --name-status');
46
      }
47
48
49
      $process->run();
50
      if (!$process->isSuccessful()) {
51
        return $filesCollection;
52
      }
53
54
55
      $filesList = array_filter(explode("\n", $process->getOutput()));
56
57
      $statusMap = [
58
        'A' => File::STATUS_ADDED,
59
        'C' => File::STATUS_COPIED,
60
        'M' => File::STATUS_MODIFIED,
61
        'R' => File::STATUS_RENAMED,
62
        'D' => File::STATUS_DELETED,
63
      ];
64
65
      foreach ($filesList as $fileInfo) {
66
67
        preg_match('!^([^\s]+)\s+(.+)$!', $fileInfo, $matchedFileInfo);
68
        if (empty($matchedFileInfo[2])) {
69
          continue;
70
        }
71
        $gitStatus = trim($matchedFileInfo[1]);
72
73
        $status = !empty($statusMap[$gitStatus]) ? $statusMap[$gitStatus] : File::STATUS_UNKNOWN;
74
75
        $fullPath = $baseDir . ltrim($matchedFileInfo[2], DIRECTORY_SEPARATOR);
76
        $filesCollection->add(new File($fullPath, $status));
77
      }
78
79
      return $filesCollection;
80
81
    }
82
83
  }