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 (#48)
by joseph
02:18
created

CheckAnnotations::checkLarge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
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): void
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): void
99
    {
100 4
        $contents = (string)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 ('' === $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
            /* Found the annotation - continue */
117 4
            if (false !== strpos($docblock, '@'.$annotation)) {
118 4
                continue;
119
            }
120
            /* No @test annotation found, not a test so continue */
121 4
            if (false === strpos($docblock, '@test')) {
122 1
                continue;
123
            }
124 3
            $this->errors[$fileInfo->getFilename()][] =
125 3
                'Failed finding @'.$annotation.' for method: '.$matches['method'][$key];
126
        }
127 4
    }
128
129
    /**
130
     * @param string $path
131
     *
132
     * @return \Generator|\SplFileInfo[]
133
     */
134 4
    private function yieldTestFilesInPath(string $path): \Generator
135
    {
136 4
        $recursiveDirectoryIterator = new \RecursiveDirectoryIterator($path);
137 4
        $iterator                   = new \RecursiveIteratorIterator($recursiveDirectoryIterator);
138 4
        foreach ($iterator as $fileInfo) {
139 4
            yield $fileInfo;
140
        }
141 4
    }
142
}
143