Completed
Push — master ( 8b81c8...699ba6 )
by Shcherbak
10:19
created

RedundantNullPropertyValueFixer::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 0
cts 0
cp 0
rs 9.2
cc 4
eloc 10
nc 4
nop 2
crap 20
1
<?
0 ignored issues
show
Security Best Practice introduced by
It is not recommend to use PHP's short opening tag <?, better use <?php, or <?= in case of outputting.

Short opening tags are disabled in PHP’s default configuration. In such a case, all content of this file is output verbatim to the browser without being parsed, or executed.

As a precaution to avoid these problems better use the long opening tag <?php.

Loading history...
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($file))->notDeleted()->extension(['php'])->isValid($file);
0 ignored issues
show
Unused Code introduced by
The call to FileFilter::__construct() has too many arguments starting with $file.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
49
    }
50
51
52
    /**
53
     * @param File $file
54
     * @param Report $report
55
     */
56
    public function process(File $file, Report $report) {
57
      $collection = Collection::createFromString($file->getContent()->get());
58
59
60
      $invalidProperties = InvalidPropertyFinder::find($collection);
61
      if (count($invalidProperties) == 0) {
62
        return;
63
      }
64
65
      foreach ($invalidProperties as $property) {
66
        $report->addMessage($file, $this, 'Replace redundant NULL value', $property->getVariable()->getLine());
67
        foreach ($property->getTokensToReplace() as $token) {
68
          $token->remove();
69
        }
70
      }
71
72
      $file->getContent()->set($collection->assemble());
73
    }
74
  }