GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#39)
by joseph
12:13
created

CheckAnnotations::checkFile()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0378

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 4
nop 2
dl 0
loc 23
ccs 13
cts 15
cp 0.8667
crap 4.0378
rs 8.7972
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\PHPQA\PHPUnit;
4
5
/**
6
 * Class CheckForLargeAndMediumAnnotations
7
 *
8
 * This class checks a test directory structure and if it is using the Edmonds Commerce recommended style of a
9
 * `Large`,`Medium` and `Large` sub directory structure, then we will also ensure that the large and medium tests are
10
 * correctly annotated
11
 *
12
 * @package EdmondsCommerce\PHPQA\PHPUnit
13
 */
14
class CheckAnnotations
15
{
16
    /**
17
     * @var string
18
     */
19
    private $largePath;
20
    /**
21
     * @var string
22
     */
23
    private $mediumPath;
24
25
    /**
26
     * @var string
27
     */
28
    private $smallPath;
29
30
    /**
31
     * @var array
32
     */
33
    private $errors = [];
34
35
    /**
36
     * Check the Large and Medium directories, if they exist, and then assert that all tests have the correct annotation
37
     *
38
     * @param string $pathToTestsDirectory
39
     *
40
     * @return array of errors
41
     */
42 6
    public function main(string $pathToTestsDirectory): array
43
    {
44 6
        if (!is_dir($pathToTestsDirectory)) {
45 1
            throw new \InvalidArgumentException(
46 1
                '$pathToTestsDirectory "'.$pathToTestsDirectory.'" does not exist"'
47
            );
48
        }
49 5
        $this->largePath  = $pathToTestsDirectory.'/Large';
50 5
        $this->mediumPath = $pathToTestsDirectory.'/Medium';
51 5
        $this->smallPath  = $pathToTestsDirectory.'/Small';
52 5
        $this->checkLarge();
53 5
        $this->checkMedium();
54 5
        $this->checkSmall();
55
56 5
        return $this->errors;
57
    }
58
59 5
    private function checkLarge(): void
60
    {
61 5
        if (!is_dir($this->largePath)) {
62 3
            return;
63
        }
64 2
        $this->checkDirectory($this->largePath, 'large');
65 2
    }
66
67 5
    private function checkMedium(): void
68
    {
69 5
        if (!is_dir($this->mediumPath)) {
70 1
            return;
71
        }
72 4
        $this->checkDirectory($this->mediumPath, 'medium');
73 4
    }
74
75 5
    private function checkSmall(): void
76
    {
77 5
        if (!is_dir($this->smallPath)) {
78 2
            return;
79
        }
80 3
        $this->checkDirectory($this->smallPath, 'small');
81 3
    }
82
83 4
    private function checkDirectory(string $path, string $annotation)
84
    {
85 4
        foreach ($this->yieldTestFilesInPath($path) as $fileInfo) {
86 4
            if (false === strpos($fileInfo->getFilename(), 'Test.php')) {
87 4
                continue;
88
            }
89 4
            $this->checkFile($fileInfo, $annotation);
90
        }
91 4
    }
92
93
    /**
94
     * @param \SplFileInfo $fileInfo
95
     * @param string       $annotation
96
     *
97
     */
98 4
    private function checkFile(\SplFileInfo $fileInfo, string $annotation)
99
    {
100 4
        $contents = file_get_contents($fileInfo->getPathname());
101 4
        $matches  = [];
102 4
        preg_match_all(
103
            <<<REGEXP
104
%(?<docblock>/\*(?:[^*]|\n|(?:\*(?:[^/]|\n)))*\*/)\s+?public\s+?function\s+?(?<method>.+?)\(%
105
REGEXP
106 4
            .'si',
107 4
            $contents,
108 4
            $matches
109
        );
110 4
        if (empty($matches[0])) {
111
            $this->errors[$fileInfo->getFilename()][] = 'Failed finding any doc blocks';
112
113
            return;
114
        }
115 4
        foreach ($matches['docblock'] as $key => $docblock) {
116 4
            if (false !== strpos($docblock, '@'.$annotation)) {
117 4
                continue;
118
            }
119 3
            $this->errors[$fileInfo->getFilename()][] =
120 3
                'Failed finding @'.$annotation.' for method: '.$matches['method'][$key];
121
        }
122 4
    }
123
124
    /**
125
     * @param string $path
126
     *
127
     * @return \Generator|\SplFileInfo[]
128
     */
129 4
    private function yieldTestFilesInPath(string $path): \Generator
130
    {
131 4
        $recursiveDirectoryIterator = new \RecursiveDirectoryIterator($path);
132 4
        $iterator                   = new \RecursiveIteratorIterator($recursiveDirectoryIterator);
133 4
        foreach ($iterator as $fileInfo) {
134 4
            yield $fileInfo;
135
        }
136 4
    }
137
}
138