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 (#85)
by
unknown
01:07
created

Phpunit::determineAutoFilter()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.1128
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
3
namespace Spatie\PhpUnitWatcher\Screens;
4
5
use Symfony\Component\Process\Process;
6
use Spatie\PhpUnitWatcher\Notification;
7
8
class Phpunit extends Screen
9
{
10
    const DEFAULT_BINARY_PATH = 'vendor/bin/phpunit';
11
12
    /** @var array */
13
    public $options;
14
15
    /** @var string */
16
    protected $phpunitArguments;
17
18
    /** @var string */
19
    private $phpunitBinaryPath;
20
21
    public function __construct(array $options)
22
    {
23
        $this->options = $options;
24
25
        $this->phpunitArguments = $options['phpunit']['arguments'] ?? '';
26
27
        $this->phpunitBinaryPath = $options['phpunit']['binaryPath'] ?? self::DEFAULT_BINARY_PATH;
28
    }
29
30
    public function draw(array $changedFilePaths = [])
31
    {
32
        $this->determineAutoFilter($changedFilePaths);
33
34
        if (! $this->options['autoFilter'] || ($this->options['autoFilter'] && ! empty($changedFilePaths))) {
35
            $this
36
                ->writeHeader()
37
                ->runTests();
38
        }
39
40
        if (! $this->options['autoFilter']) {
41
            $this->displayManual();
42
        }
43
    }
44
45
    public function registerListeners()
46
    {
47
        if ($this->options['autoFilter']) {
48
            return $this;
49
        }
50
51
        $this->terminal->onKeyPress(function ($line) {
52
            $line = strtolower($line);
53
54
            switch ($line) {
55
                case '':
56
                    $this->terminal->refreshScreen();
57
                    break;
58
                case 'a':
59
                    $this->options['phpunit']['arguments'] = '';
60
61
                    $this->terminal->displayScreen(new Phpunit($this->options));
62
                    break;
63
                case 'g':
64
                    $this->terminal->displayScreen(new FilterGroupName());
65
                    break;
66
                case 's':
67
                    $this->terminal->displayScreen(new FilterTestSuiteName());
68
                    break;
69
                case 't':
70
                    $this->terminal->displayScreen(new FilterTestName());
71
                    break;
72
                case 'p':
73
                    $this->terminal->displayScreen(new FilterFileName());
74
                    break;
75
                case 'q':
76
                    die();
77
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
78
                default:
79
                    $this->registerListeners();
80
                    break;
81
            }
82
        });
83
84
        return $this;
85
    }
86
87
    protected function writeHeader()
88
    {
89
        $title = 'Starting PHPUnit';
90
91
        if (! empty($this->phpunitArguments)) {
92
            $title .= " with arguments: `{$this->phpunitArguments}`";
93
        }
94
95
        $this->terminal
96
            ->comment($title)
97
            ->emptyLine();
98
99
        return $this;
100
    }
101
102
    protected function runTests()
103
    {
104
        $result = (new Process("{$this->phpunitBinaryPath} {$this->phpunitArguments}"))
105
            ->setTty(true)
106
            ->run(function ($type, $line) {
107
                echo $line;
108
            });
109
110
        $this->sendDesktopNotification($result);
111
112
        return $this;
113
    }
114
115
    protected function displayManual()
116
    {
117
        if ($this->options['hideManual']) {
118
            return $this;
119
        }
120
121
        $this->terminal
122
            ->emptyLine()
123
            ->write('<dim>Press </dim>a<dim> to run all tests.</dim>')
124
            ->write('<dim>Press </dim>t<dim> to filter by test name.</dim>')
125
            ->write('<dim>Press </dim>p<dim> to filter by file name.</dim>')
126
            ->write('<dim>Press </dim>g<dim> to filter by group name.</dim>')
127
            ->write('<dim>Press </dim>s<dim> to filter by test suite name.</dim>')
128
            ->write('<dim>Press </dim>q<dim> to quit the watcher.</dim>')
129
            ->write('<dim>Press </dim>Enter<dim> to trigger a test run.</dim>');
130
131
        return $this;
132
    }
133
134
    protected function sendDesktopNotification(int $result)
135
    {
136
        $notificationName = $result === 0
137
            ? 'passingTests'
138
            : 'failingTests';
139
140
        if ($this->options['notifications'][$notificationName]) {
141
            Notification::create()->$notificationName();
142
        }
143
    }
144
145
    public function determineAutoFilter(array $changedFilePaths = [])
146
    {
147
        $autoFilterOption = null;
0 ignored issues
show
Unused Code introduced by
$autoFilterOption is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
148
        $this->phpunitArguments = isset($this->options['phpunit']['arguments']) ? $this->options['phpunit']['arguments'] : '';
149
150
        // Apply a filter based on the changed files
151
        if (! empty($changedFilePaths)) {
152
            $testNames = array_map(function ($filePath) {
153
                $filePathParts = explode('/', $filePath);
154
                $fileName = end($filePathParts);
155
                $fileNameParts = explode('.', $fileName);
156
157
                $testName = current($fileNameParts);
158
159
                // Suffix with "Test" if it's not already a test
160
                $strlen = strlen($testName);
161
                if ($strlen < 4 || ! (substr_compare(strtolower($testName), 'test', $strlen - 4, 4) === 0)) {
162
                    $testName .= 'Test';
163
                }
164
165
                return $testName;
166
            }, $changedFilePaths);
167
168
            $testFilterPattern = '/('.implode('|', $testNames).')/';
169
            $autoFilterOption = " --filter=\"$testFilterPattern\"";
170
171
            $this->phpunitArguments .= $autoFilterOption;
172
        }
173
174
        return $this;
175
    }
176
177
    public function getPhpunitArguments()
178
    {
179
        return isset($this->phpunitArguments) ? $this->phpunitArguments : null;
180
    }
181
}
182