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

CodeValidator::writePhpStanAutoloader()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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