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:24
created

RerunCommandGenerator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 11 3
C main() 0 34 7
A getDefaultFilePath() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\PHPQA\PHPUnit;
4
5
use EdmondsCommerce\PHPQA\Helper;
6
7
class RerunCommandGenerator
8
{
9
    /**
10
     * no failed tests so just include all
11
     * however, this can confuse bash - try using `set -f` to disable globbing
12
     */
13
    public const NO_FILTER = '/.*/';
14
15
    /**
16
     * @var string
17
     */
18
    private $logPath;
19
20
    /**
21
     * @var \SimpleXMLElement
22
     */
23
    private $simpleXml;
24
25
    private $toRerun = [];
26
27
    public function main(string $junitLogPath = null): string
28
    {
29
        $this->toRerun = [];
30
        $this->logPath = $junitLogPath ?? $this->getDefaultFilePath();
31
        if (!file_exists($this->logPath)) {
32
            return self::NO_FILTER;
33
        }
34
        $contents = file_get_contents($this->logPath);
35
        if ('' === $contents) {
36
            return self::NO_FILTER;
37
        }
38
        $this->load($contents);
39
        $failureNodes = $this->simpleXml->xpath(
40
            '//testsuite/testcase[error] | //testsuite/testcase[failure] '
41
            .'| //testsuite/testcase[skipped] | //testsuite/testcase[incomplete]'
42
        );
43
        foreach ($failureNodes as $testCaseNode) {
44
            $attributes              = $testCaseNode->attributes();
45
            $class                   = str_replace('\\', '\\\\', (string)$attributes->class);
46
            $this->toRerun[$class][] = (string)$attributes->name;
47
        }
48
        if ($this->toRerun === []) {
49
            return self::NO_FILTER;
50
        }
51
        $command = '/(';
52
        foreach ($this->toRerun as $class => $testNames) {
53
            foreach ($testNames as $testName) {
54
                $command .= "$class::$testName|";
55
            }
56
        }
57
        $command = rtrim($command, '|');
58
        $command .= ')/';
59
60
        return $command;
61
    }
62
63
64
    protected function load(string $contents)
65
    {
66
67
        libxml_use_internal_errors(true);
68
        $this->simpleXml = simplexml_load_string($contents);
0 ignored issues
show
Documentation Bug introduced by
It seems like simplexml_load_string($contents) can also be of type false. However, the property $simpleXml is declared as type SimpleXMLElement. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
69
        if (false === $this->simpleXml) {
70
            $message = "Failed loading XML\n";
71
            foreach (libxml_get_errors() as $error) {
72
                $message .= "\n\t".$error->message;
73
            }
74
            throw new \RuntimeException($message);
75
        }
76
    }
77
78
    /**
79
     * @return string
80
     * @throws \Exception
81
     * @SuppressWarnings(PHPMD.StaticAccess)
82
     */
83
    protected function getDefaultFilePath(): string
84
    {
85
        return Helper::getProjectRootDirectory().'/var/qa/phpunit.junit.log.xml';
86
    }
87
}
88