Completed
Push — master ( 4a09ca...58b9b1 )
by Shcherbak
05:17 queued 01:09
created

RedundantNullPropertyValueFixer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 73.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 0
cbo 8
dl 0
loc 62
ccs 11
cts 15
cp 0.7332
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getDescription() 0 3 1
A canProcess() 0 3 1
A process() 0 18 4
1
<?php
2
3
  namespace Funivan\Cs\Tools\Php\RedundantNullPropertyValue;
4
5
  use Funivan\Cs\FileTool\FileTool;
6
  use Funivan\Cs\Fs\File;
7
  use Funivan\Cs\Fs\FileFilter;
8
  use Funivan\Cs\Report\Report;
9
  use Funivan\PhpTokenizer\Collection;
10
11
  /**
12
   *
13
   */
14
  class RedundantNullPropertyValueFixer implements FileTool {
15
16
17
    /**
18
     * Return unique string of the tool
19
     * You can set any name but we suggest to use following rules:
20
     *  - Allowed chars [a-z0-9_]+
21
     *  - Review tools should have ending `_review`
22
     *  - Fixer tools should have ending `_fixer`
23
     *
24
     * @codeCoverageIgnore
25
     * @return string
26
     */
27
    public function getName() {
28
      return 'redundant_null_property_value_fixer';
29
    }
30
31
32
    /**
33
     * @return string
34
     */
35
    public function getDescription() {
36
      return 'Replace null assignment from the class property';
37
    }
38
39
40
    /**
41
     * Check if we can process file by this tool
42
     * Called before file process
43
     *
44
     * @param File $file
45
     * @return boolean
46
     */
47
    public function canProcess(File $file) {
48
      return (new FileFilter())->notDeleted()->extension(['php'])->isValid($file);
49
    }
50
51
52
    /**
53
     * @param File $file
54
     * @param Report $report
55
     */
56 10
    public function process(File $file, Report $report) {
57 10
      $collection = Collection::createFromString($file->getContent()->get());
58
59
60 10
      $invalidProperties = InvalidPropertyFinder::find($collection);
61 10
      if (count($invalidProperties) === 0) {
62 3
        return;
63
      }
64
65 7
      foreach ($invalidProperties as $property) {
66 7
        $report->addMessage($file, $this, 'Replace redundant NULL value', $property->getVariable()->getLine());
67 7
        foreach ($property->getTokensToReplace() as $token) {
68 7
          $token->remove();
69
        }
70
      }
71
72 7
      $file->getContent()->set($collection->assemble());
73 7
    }
74
75
  }