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 (#38)
by joseph
02:23 queued 15s
created

CheckForLargeAndMediumAnnotations::main()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 9.4285
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 CheckForLargeAndMediumAnnotations
15
{
16
    /**
17
     * @var string
18
     */
19
    private $largePath;
20
    /**
21
     * @var string
22
     */
23
    private $mediumPath;
24
25
    /**
26
     * @var array
27
     */
28
    private $errors = [];
29
30
    /**
31
     * Check the Large and Medium directories, if they exist, and then assert that all tests have the correct annotation
32
     *
33
     * @param string $pathToTestsDirectory
34
     *
35
     * @return array of errors
36
     */
37
    public function main(string $pathToTestsDirectory): array
38
    {
39
        if (!is_dir($pathToTestsDirectory)) {
40
            throw new \InvalidArgumentException(
41
                '$pathToTestsDirectory "'.$pathToTestsDirectory.'" does not exist"'
42
            );
43
        }
44
        $this->largePath  = $pathToTestsDirectory.'/Large';
45
        $this->mediumPath = $pathToTestsDirectory.'/Medium';
46
        $this->checkLarge();
47
        $this->checkMedium();
48
49
        return $this->errors;
50
    }
51
52
    private function checkLarge(): void
53
    {
54
        if (!is_dir($this->largePath)) {
55
            return;
56
        }
57
        $this->checkDirectory($this->largePath, 'large');
58
    }
59
60
    private function checkMedium(): void
61
    {
62
        if (!is_dir($this->mediumPath)) {
63
            return;
64
        }
65
        $this->checkDirectory($this->mediumPath, 'medium');
66
    }
67
68
    private function checkDirectory(string $path, string $annotation)
69
    {
70
        foreach ($this->yieldTestFilesInPath($path) as $fileInfo) {
71
            if (false === strpos($fileInfo->getFilename(), 'Test.php')) {
72
                continue;
73
            }
74
            $this->checkFile($fileInfo, $annotation);
75
        }
76
    }
77
78
    /**
79
     * @param \SplFileInfo $fileInfo
80
     * @param string       $annotation
81
     *
82
     */
83
    private function checkFile(\SplFileInfo $fileInfo, string $annotation)
84
    {
85
        $contents = file_get_contents($fileInfo->getPathname());
86
        $matches  = [];
87
        preg_match_all(
88
            <<<REGEXP
89
%(?<docblock>/\*(?:[^*]|\n|(?:\*(?:[^/]|\n)))*\*/)\s+?public\s+?function\s+?(?<method>.+?)\(%
90
REGEXP
91
            .'si',
92
            $contents,
93
            $matches
94
        );
95
        if (empty($matches[0])) {
96
            $this->errors[$fileInfo->getFilename()][] = 'Failed finding any doc blocks';
97
98
            return;
99
        }
100
        foreach ($matches['docblock'] as $key => $docblock) {
101
            if (false !== strpos($docblock, '@'.$annotation)) {
102
                continue;
103
            }
104
            $this->errors[$fileInfo->getFilename()][] =
105
                'Failed finding @'.$annotation.' for method: '.$matches['method'][$key];
106
        }
107
    }
108
109
    /**
110
     * @param string $path
111
     *
112
     * @return \Generator|\SplFileInfo[]
113
     */
114
    private function yieldTestFilesInPath(string $path): \Generator
115
    {
116
        $recursiveDirectoryIterator = new \RecursiveDirectoryIterator($path);
117
        $iterator                   = new \RecursiveIteratorIterator($recursiveDirectoryIterator);
118
        foreach ($iterator as $fileInfo) {
119
            yield $fileInfo;
120
        }
121
    }
122
}
123