FixerProcessor::getSaveFiles()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
  namespace Funivan\Cs\FileProcessor;
4
5
  use Funivan\Cs\Fs\FilesCollection;
6
  use Funivan\Cs\Report\Report;
7
8
  /**
9
   * @author Ivan Shcherbak <[email protected]> 2016
10
   */
11
  class FixerProcessor extends BaseFileProcessor {
12
13
    /**
14
     * @var bool
15
     */
16
    private $saveFiles = false;
17
18
19
    /**
20
     * @param FilesCollection $files
21
     * @param Report $report
22
     */
23
    public function process(FilesCollection $files, Report $report) {
24
25
      if (empty($this->getTools())) {
26
        $this->getOutput()->writeln('<comment>Empty tools list</comment>');
27
        return;
28
      }
29
30
      $message = '✘ dry run  ';
31
      if ($this->saveFiles) {
32
        $message = '✔ fixed    ';
33
      }
34
35
      foreach ($files as $file) {
36
37
        $fixed = false;
38
39
        foreach ($this->getTools() as $tool) {
40
41
          if ($this->getOutput()->isDebug()) {
42
            $this->getOutput()->writeln('process : (' . $tool->getName() . ') ' . $file->getPath());
43
          }
44
45
          if (!$tool->canProcess($file)) {
46
            continue;
47
          }
48
49
          $tool->process($file, $report);
50
51
          $fileChanged = $file->getContent()->isChanged();
52
          if ($fileChanged === false) {
53
            continue;
54
          }
55
56
          $fixed = true;
57
58
          $this->getOutput()->writeln('<info>' . $message . ': ' . $tool->getDescription() . '</info>');
59
        }
60
61
        if ($fixed and $this->saveFiles) {
62
          $file->save();
63
          $this->getOutput()->writeln('<info>' . '✔ saved    : ' . $file->getPath() . '</info>');
64
        }
65
66
      }
67
68
    }
69
70
71
    /**
72
     * @return boolean
73
     */
74
    public function getSaveFiles() {
75
      return $this->saveFiles;
76
    }
77
78
79
    /**
80
     * @param boolean $saveFiles
81
     */
82
    public function setSaveFiles($saveFiles) {
83
      $this->saveFiles = $saveFiles;
84
    }
85
86
  }