StandardFileFinder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 71
c 0
b 0
f 0
wmc 9
lcom 0
cbo 5
ccs 0
cts 45
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C findFiles() 0 62 9
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') !== null) {
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');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $baseDir is correct as $finderParameters->get('baseDir') (which targets Funivan\Cs\Fs\FileFinder\FinderParameters::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
38
      if ($baseDir == null) {
39
        throw new \RuntimeException('Can not find files. Invalid baseDir finder parameter.');
40
      }
41
      $baseDir = rtrim($baseDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
42
      $commit = $finderParameters->get('commit');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $commit is correct as $finderParameters->get('commit') (which targets Funivan\Cs\Fs\FileFinder\FinderParameters::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

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