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

setRelativePathFromWorkDirToProjectRoot()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Assets;
4
5
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\AbstractCommand;
6
use EdmondsCommerce\DoctrineStaticMeta\Tests\Large\FullProjectBuildLargeTest;
7
use Overtrue\PHPLint\Linter;
8
9
class CodeValidator
10
{
11
    private $pathToWorkDir;
12
    private $relativePathToProjectRoot;
13
    private $namespaceRoot;
14
15
    public function __invoke(string $pathToWorkDir, string $namespaceRoot): ?string
16
    {
17
        $this->namespaceRoot = $namespaceRoot;
18
        $this->setRealPathToWorkDir($pathToWorkDir);
19
        $this->setRelativePathFromWorkDirToProjectRoot();
20
        $this->writePhpStanAutoloader();
21
        $lintErrors = $this->runPhpLint();
22
        if (null !== $lintErrors) {
23
            return $lintErrors;
24
        }
25
        $stanErrors = $this->runPhpStan();
26
        if (null !== $stanErrors) {
27
            return $stanErrors;
28
        }
29
30
        return null;
31
    }
32
33
    private function setRealPathToWorkDir(string $pathToWorkDir): void
34
    {
35
        $realpath = \realpath($pathToWorkDir);
36
        if (false === $realpath) {
37
            throw new \RuntimeException('Path ' . $pathToWorkDir . ' does not exist');
38
        }
39
        $this->pathToWorkDir = $realpath;
40
    }
41
42
    private function setRelativePathFromWorkDirToProjectRoot(): void
43
    {
44
        $currentPath  = $this->pathToWorkDir;
45
        $relativePath = './';
46
        while (false === is_dir("$currentPath/$relativePath/vendor")) {
47
            $relativePath .= '../';
48
            if (false === \realpath("$currentPath/$relativePath")) {
49
                throw new \RuntimeException('Failed finding the relative path to the vendor directory');
50
            }
51
        }
52
        $this->relativePathToProjectRoot = $relativePath;
53
    }
54
55
    private function writePhpStanAutoloader(): void
56
    {
57
        $phpstanNamespace  = $this->namespaceRoot . '\\\\';
58
        $srcFolder         = $this->pathToWorkDir . '/' . AbstractCommand::DEFAULT_SRC_SUBFOLDER;
59
        $testsFolder       = $this->pathToWorkDir . '/' . AbstractCommand::DEFAULT_TEST_SUBFOLDER;
60
        $phpstanAutoLoader = '<?php declare(strict_types=1);
61
require __DIR__."' . $this->relativePathToProjectRoot . '/vendor/autoload.php";
62
63
use Composer\Autoload\ClassLoader;
64
65
$loader = new class extends ClassLoader
66
{
67
    public function loadClass($class)
68
    {
69
        if (false === strpos($class, "' . $this->namespaceRoot . '")) {
70
            return false;
71
        }
72
        $found = parent::loadClass($class);
73
        if (\in_array(gettype($found), [\'boolean\', \'NULL\'], true)) {
74
            //good spot to set a break point ;)
75
            return false;
76
        }
77
78
        return true;
79
    }
80
};
81
$loader->addPsr4(
82
    "' . $phpstanNamespace . '","' . $srcFolder . '"
83
);
84
$loader->addPsr4(
85
    "' . $phpstanNamespace . '","' . $testsFolder . '"
86
);
87
$loader->register();
88
';
89
        file_put_contents($this->pathToWorkDir . '/phpstan-autoloader.php', $phpstanAutoLoader);
90
    }
91
92
    private function runPhpLint(): ?string
93
    {
94
        $exclude    = ['vendor'];
95
        $extensions = ['php'];
96
        $linter     = new Linter($this->pathToWorkDir, $exclude, $extensions);
97
        $lint       = $linter->lint([], false);
98
        if (empty($lint)) {
99
            return null;
100
        }
101
        $message = str_replace($this->pathToWorkDir, '', print_r($lint, true));
102
103
        return "\n\nPHP Syntax Errors in $this->pathToWorkDir\n\n$message\n\n";
104
    }
105
106
    private function runPhpStan(): ?string
107
    {
108
        $pathToBin      = __DIR__ . '/../../bin';
109
        $phpstanCommand = FullProjectBuildLargeTest::BASH_PHPNOXDEBUG_FUNCTION
110
                          . "\n\nphpNoXdebug $pathToBin/phpstan.phar ";
111
        if ($this->isTravis()) {
112
            $phpstanCommand = 'bin/phpstan.phar ';
113
        }
114
        $phpstanCommand .= "analyse $this->pathToWorkDir/ -l7 "
115
                           . ' --no-progress '
116
                           . "-a $this->pathToWorkDir/phpstan-autoloader.php 2>&1";
117
        exec(
118
            $phpstanCommand,
119
            $output,
120
            $exitCode
121
        );
122
        if (0 === $exitCode) {
123
            return null;
124
        }
125
126
        return 'PHPStan errors found in generated code at ' . $this->pathToWorkDir
127
               . ':' . "\n\n" . implode("\n", $output);
128
    }
129
130
    /**
131
     * @SuppressWarnings(PHPMD.Superglobals)
132
     * @return bool
133
     */
134
    protected function isTravis(): bool
135
    {
136
        return isset($_SERVER['TRAVIS']);
137
    }
138
}
139