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 (#54)
by joseph
28:13 queued 24:17
created

CodeValidator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 122
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A runPhpLint() 0 12 2
A setRelativePathFromWorkDirToProjectRoot() 0 11 3
A runPhpStan() 0 20 3
A __invoke() 0 16 3
B writePhpStanAutoloader() 0 31 1
A setRealPathToWorkDir() 0 7 2
A isTravis() 0 3 1
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 analyse $this->pathToWorkDir/src -l7 -a "
105
                          .$this->pathToWorkDir.'/phpstan-autoloader.php 2>&1';
106
        if ($this->isTravis()) {
107
            $phpstanCommand = "bin/phpstan.phar analyse $this->pathToWorkDir/src -l7 -a "
108
                              .$this->pathToWorkDir.'/phpstan-autoloader.php 2>&1';
109
        }
110
        exec(
111
            $phpstanCommand,
112
            $output,
113
            $exitCode
114
        );
115
        if (0 === $exitCode) {
116
            return null;
117
        }
118
119
        return 'PHPStan errors found in generated code at '.$this->pathToWorkDir
120
               .':'."\n\n".implode("\n", $output);
121
    }
122
123
    /**
124
     * @SuppressWarnings(PHPMD.Superglobals)
125
     * @return bool
126
     */
127
    protected function isTravis(): bool
128
    {
129
        return isset($_SERVER['TRAVIS']);
130
    }
131
}
132