FilesRule   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 0
loc 135
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B evaluate() 0 21 6
A expectsFilesGlobExists() 0 18 2
A globShouldFind() 0 10 2
A globShouldNotFind() 0 8 2
A expectsFilesGrep() 0 10 4
C fileShouldGrep() 0 22 7
1
<?php
2
/*
3
 * This file is part of project-quality-inspector.
4
 *
5
 * (c) Alexandre GESLIN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace ProjectQualityInspector\Rule;
12
13
use ProjectQualityInspector\Application\ProcessHelper;
14
use ProjectQualityInspector\Exception\ExpectationFailedException;
15
16
/**
17
 * Class FilesRule
18
 *
19
 * @package ProjectQualityInspector\Rule
20
 */
21
class FilesRule extends AbstractRule
22
{
23
    public function __construct(array $config, $baseDir)
24
    {
25
        parent::__construct($config, $baseDir);
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function evaluate()
32
    {
33
        $expectationsFailedExceptions = [];
34
35
        foreach ($this->config as $fileConf) {
36
            try {
37
                $files = $this->expectsFilesGlobExists($fileConf, $this->baseDir);
38
                if (isset($fileConf['grep']) && count($files)) {
39
                    $this->expectsFilesGrep($files, $fileConf['grep'], $this->getReason($fileConf));
0 ignored issues
show
Bug introduced by
It seems like $this->getReason($fileConf) targeting ProjectQualityInspector\...stractRule::getReason() can also be of type array or null; however, ProjectQualityInspector\...ule::expectsFilesGrep() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
40
                }
41
                $this->addAssertion($this->getValue($fileConf));
0 ignored issues
show
Bug introduced by
It seems like $this->getValue($fileConf) targeting ProjectQualityInspector\...bstractRule::getValue() can also be of type array; however, ProjectQualityInspector\...actRule::addAssertion() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
42
            } catch (ExpectationFailedException $e) {
43
                $expectationsFailedExceptions[] = $e;
44
                $this->addAssertion($this->getValue($fileConf), [['message' => $e->getMessage() . $e->getReason(), 'type' => 'expectsFilesGlobExists|expectsFilesGrep']]);
0 ignored issues
show
Bug introduced by
It seems like $this->getValue($fileConf) targeting ProjectQualityInspector\...bstractRule::getValue() can also be of type array; however, ProjectQualityInspector\...actRule::addAssertion() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
45
            }
46
        }
47
48
        if (count($expectationsFailedExceptions)) {
49
            $this->throwRuleViolationException($expectationsFailedExceptions);
50
        }
51
    }
52
53
    /**
54
     * @param $fileConf
55
     * @param $baseDir
56
     * @return array
57
     */
58
    private function expectsFilesGlobExists($fileConf, $baseDir)
59
    {
60
        $files = [];
61
        $fileName = $this->getValue($fileConf);
62
        $reason = $this->getReason($fileConf);
63
64
        $filePath = $baseDir . DIRECTORY_SEPARATOR . $fileName;
65
66
        if ($fileName[0] == '!') {
67
            $fileName = ltrim($fileName, '!');
68
            $filePath = $baseDir . DIRECTORY_SEPARATOR . $fileName;
69
            $this->globShouldNotFind($filePath, $reason);
0 ignored issues
show
Bug introduced by
It seems like $reason defined by $this->getReason($fileConf) on line 62 can also be of type array or null; however, ProjectQualityInspector\...le::globShouldNotFind() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
70
        } else {
71
            $files = $this->globShouldFind($filePath, $reason);
72
        }
73
74
        return $files;
75
    }
76
77
    /**
78
     * @param $filePathGlob
79
     * @param $reason
80
     * @return array
81
     *
82
     * @throws ExpectationFailedException
83
     */
84
    private function globShouldFind($filePathGlob, $reason)
85
    {
86
        $message = sprintf('file <fg=green>"%s"</> should exists', $filePathGlob);
87
        $files = glob($filePathGlob);
88
        if (!count($files)) {
89
            throw new ExpectationFailedException($filePathGlob, $message, $reason);
90
        }
91
92
        return $files;
93
    }
94
95
    /**
96
     * @param string $filePathGlob
97
     * @param string $reason
98
     *
99
     * @throws ExpectationFailedException
100
     */
101
    private function globShouldNotFind($filePathGlob, $reason)
102
    {
103
        $message = sprintf('file <fg=green>"%s"</> should not exists', $filePathGlob);
104
105
        if (count(glob($filePathGlob))) {
106
            throw new ExpectationFailedException($filePathGlob, $message, $reason);
107
        }
108
    }
109
110
    /**
111
     * @param array $files
112
     * @param array|string $greps
113
     * @param string $reason
114
     */
115
    private function expectsFilesGrep(array $files, $greps, $reason)
116
    {
117
        $greps = (!is_array($greps)) ? [$greps] : $greps;
118
119
        foreach ($files as $file) {
120
            foreach ($greps as $grep) {
121
                $this->fileShouldGrep($file, $grep, $reason);
122
            }
123
        }
124
    }
125
126
    /**
127
     * @param string $filePath
128
     * @param string $grep
129
     * @param string $reason
130
     *
131
     * @throws ExpectationFailedException
132
     */
133
    private function fileShouldGrep($filePath, $grep, $reason)
134
    {
135
        $message = sprintf('file <fg=green>"%s"</> should contain <fg=green>"%s"</> string', $filePath, $grep);
136
        $negation = false;
137
        $recursiveOption = '';
138
139
        if ($grep[0] == '!') {
140
            $grep = ltrim($grep, '!');
141
            $negation = true;
142
            $message = sprintf('file <fg=green>"%s"</> should not contain <fg=green>"%s"</> string', $filePath, $grep);
143
        }
144
145
        if (is_dir($filePath)) {
146
            $recursiveOption = '-r';
147
        }
148
149
        $result = ProcessHelper::execute(sprintf('grep %s %s %s', $recursiveOption, escapeshellarg($grep), $filePath), $this->baseDir, true);
150
151
        if (($negation && count($result)) || (!$negation && !count($result))) {
152
            throw new ExpectationFailedException($filePath, $message, $reason);
153
        }
154
    }
155
}