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

Phpunit::getPhpunitArguments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
33
        $this->determineAutoFilter($changedFilePaths);
34
35
        if (!$this->options['autoFilter'] || ($this->options['autoFilter'] && !empty($changedFilePaths))) {
36
            $this
37
                ->writeHeader()
38
                ->runTests();
39
        }
40
41
        if (!$this->options['autoFilter']) {
42
            $this->displayManual();
43
        }
44
    }
45
46
    public function registerListeners()
47
    {
48
        if ($this->options['autoFilter']) {
49
            return $this;
50
        }
51
52
        $this->terminal->onKeyPress(function ($line) {
53
            $line = strtolower($line);
54
55
            switch ($line) {
56
                case '':
57
                    $this->terminal->refreshScreen();
58
                    break;
59
                case 'a':
60
                    $this->options['phpunit']['arguments'] = '';
61
62
                    $this->terminal->displayScreen(new Phpunit($this->options));
63
                    break;
64
                case 'g':
65
                    $this->terminal->displayScreen(new FilterGroupName());
66
                    break;
67
                case 's':
68
                    $this->terminal->displayScreen(new FilterTestSuiteName());
69
                    break;
70
                case 't':
71
                    $this->terminal->displayScreen(new FilterTestName());
72
                    break;
73
                case 'p':
74
                    $this->terminal->displayScreen(new FilterFileName());
75
                    break;
76
                case 'q':
77
                    die();
78
                    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...
79
                default:
80
                    $this->registerListeners();
81
                    break;
82
            }
83
        });
84
85
        return $this;
86
    }
87
88
    protected function writeHeader()
89
    {
90
        $title = 'Starting PHPUnit';
91
92
        if (! empty($this->phpunitArguments)) {
93
            $title .= " with arguments: `{$this->phpunitArguments}`";
94
        }
95
96
        $this->terminal
97
            ->comment($title)
98
            ->emptyLine();
99
100
        return $this;
101
    }
102
103
    protected function runTests()
104
    {
105
        $result = (new Process("{$this->phpunitBinaryPath} {$this->phpunitArguments}"))
106
            ->setTty(true)
107
            ->run(function ($type, $line) {
108
                echo $line;
109
            });
110
111
        $this->sendDesktopNotification($result);
112
113
        return $this;
114
    }
115
116
    protected function displayManual()
117
    {
118
        if ($this->options['hideManual']) {
119
            return $this;
120
        }
121
122
        $this->terminal
123
            ->emptyLine()
124
            ->write('<dim>Press </dim>a<dim> to run all tests.</dim>')
125
            ->write('<dim>Press </dim>t<dim> to filter by test name.</dim>')
126
            ->write('<dim>Press </dim>p<dim> to filter by file name.</dim>')
127
            ->write('<dim>Press </dim>g<dim> to filter by group name.</dim>')
128
            ->write('<dim>Press </dim>s<dim> to filter by test suite name.</dim>')
129
            ->write('<dim>Press </dim>q<dim> to quit the watcher.</dim>')
130
            ->write('<dim>Press </dim>Enter<dim> to trigger a test run.</dim>');
131
132
        return $this;
133
    }
134
135
    protected function sendDesktopNotification(int $result)
136
    {
137
        $notificationName = $result === 0
138
            ? 'passingTests'
139
            : 'failingTests';
140
141
        if ($this->options['notifications'][$notificationName]) {
142
            Notification::create()->$notificationName();
143
        }
144
    }
145
146
    public function determineAutoFilter(array $changedFilePaths = [])
147
    {
148
        $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...
149
        $this->phpunitArguments = isset($this->options['phpunit']['arguments']) ? $this->options['phpunit']['arguments'] : '';
150
151
        // Apply a filter based on the changed files
152
        if (!empty($changedFilePaths)) {
153
154
            $testNames = array_map(function($filePath) {
155
156
                $filePathParts = explode('/', $filePath);
157
                $fileName = end($filePathParts);
158
                $fileNameParts = explode('.', $fileName);
159
160
                $testName = current($fileNameParts);
161
162
                // Suffix with "Test" if it's not already a test
163
                $strlen = strlen($testName);
164
                if ($strlen < 4 || !(substr_compare(strtolower($testName), 'test', $strlen - 4, 4) === 0)) {
165
                    $testName .= 'Test';
166
                }
167
168
                return $testName;
169
            }, $changedFilePaths);
170
171
            $testFilterPattern = '/(' . implode('|', $testNames) . ')/';
172
            $autoFilterOption = " --filter=\"$testFilterPattern\"";
173
174
            $this->phpunitArguments .= $autoFilterOption;
175
        }
176
177
        return $this;
178
    }
179
180
    public function getPhpunitArguments()
181
    {
182
        return isset($this->phpunitArguments) ? $this->phpunitArguments : null;
183
    }
184
}
185