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

StandardFileFinder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 70
wmc 9
lcom 0
cbo 5
ccs 0
cts 44
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C findFiles() 0 61 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')) {
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
  }