nunomaduro /
collision
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This file is part of Collision. |
||
| 5 | * |
||
| 6 | * (c) Nuno Maduro <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace NunoMaduro\Collision\Adapters\Phpunit; |
||
| 13 | |||
| 14 | use NunoMaduro\Collision\Writer; |
||
| 15 | use PHPUnit\Framework\AssertionFailedError; |
||
| 16 | use PHPUnit\Framework\ExceptionWrapper; |
||
| 17 | use PHPUnit\Framework\ExpectationFailedException; |
||
| 18 | use PHPUnit\Framework\TestCase; |
||
| 19 | use Symfony\Component\Console\Output\ConsoleOutput; |
||
| 20 | use Symfony\Component\Console\Output\ConsoleSectionOutput; |
||
| 21 | use Throwable; |
||
| 22 | use Whoops\Exception\Inspector; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @internal |
||
| 26 | */ |
||
| 27 | final class Style |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var ConsoleOutput |
||
| 31 | */ |
||
| 32 | private $output; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var ConsoleSectionOutput |
||
| 36 | */ |
||
| 37 | private $footer; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Style constructor. |
||
| 41 | */ |
||
| 42 | 3 | public function __construct(ConsoleOutput $output) |
|
| 43 | { |
||
| 44 | 3 | $this->output = $output; |
|
| 45 | |||
| 46 | 3 | $this->footer = $output->section(); |
|
| 47 | 3 | } |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Prints the content similar too:. |
||
| 51 | * |
||
| 52 | * ``` |
||
| 53 | * PASS Unit\ExampleTest |
||
| 54 | * ✓ basic test |
||
| 55 | * ``` |
||
| 56 | */ |
||
| 57 | public function writeCurrentRecap(State $state): void |
||
| 58 | { |
||
| 59 | if (!$state->testCaseTestsCount()) { |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | |||
| 63 | $this->footer->clear(); |
||
| 64 | |||
| 65 | $this->output->writeln($this->titleLineFrom( |
||
| 66 | $state->getTestCaseTitle() === 'FAIL' ? 'white' : 'black', |
||
| 67 | $state->getTestCaseTitleColor(), |
||
| 68 | $state->getTestCaseTitle(), |
||
| 69 | $state->testCaseName |
||
| 70 | )); |
||
| 71 | |||
| 72 | $state->eachTestCaseTests(function (TestResult $testResult) { |
||
| 73 | $this->output->writeln($this->testLineFrom( |
||
| 74 | $testResult->color, |
||
| 75 | $testResult->icon, |
||
| 76 | $testResult->description, |
||
| 77 | $testResult->warning |
||
| 78 | )); |
||
| 79 | }); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Prints the content similar too on the footer. Where |
||
| 84 | * we are updating the current test. |
||
| 85 | * |
||
| 86 | * ``` |
||
| 87 | * Runs Unit\ExampleTest |
||
| 88 | * • basic test |
||
| 89 | * ``` |
||
| 90 | */ |
||
| 91 | public function updateFooter(State $state, TestCase $testCase = null): void |
||
| 92 | { |
||
| 93 | $runs = []; |
||
| 94 | |||
| 95 | if ($testCase) { |
||
| 96 | $runs[] = $this->titleLineFrom( |
||
| 97 | 'black', |
||
| 98 | 'yellow', |
||
| 99 | 'RUNS', |
||
| 100 | get_class($testCase) |
||
| 101 | ); |
||
| 102 | |||
| 103 | $testResult = TestResult::fromTestCase($testCase, TestResult::RUNS); |
||
| 104 | $runs[] = $this->testLineFrom( |
||
| 105 | $testResult->color, |
||
| 106 | $testResult->icon, |
||
| 107 | $testResult->description |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | |||
| 111 | $types = [TestResult::FAIL, TestResult::WARN, TestResult::RISKY, TestResult::INCOMPLETE, TestResult::SKIPPED, TestResult::PASS]; |
||
| 112 | |||
| 113 | foreach ($types as $type) { |
||
| 114 | if ($countTests = $state->countTestsInTestSuiteBy($type)) { |
||
| 115 | $color = TestResult::makeColor($type); |
||
| 116 | $tests[] = "<fg=$color;options=bold>$countTests $type</>"; |
||
|
0 ignored issues
–
show
|
|||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | $pending = $state->suiteTotalTests - $state->testSuiteTestsCount(); |
||
| 121 | if ($pending) { |
||
| 122 | $tests[] = "\e[2m$pending pending\e[22m"; |
||
|
0 ignored issues
–
show
The variable
$tests does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
| 123 | } |
||
| 124 | |||
| 125 | if (!empty($tests)) { |
||
| 126 | $this->footer->overwrite(array_merge($runs, [ |
||
| 127 | '', |
||
| 128 | sprintf( |
||
| 129 | ' <fg=white;options=bold>Tests: </><fg=default>%s</>', |
||
| 130 | implode(', ', $tests) |
||
| 131 | ), |
||
| 132 | ])); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Writes the final recap. |
||
| 138 | */ |
||
| 139 | public function writeRecap(Timer $timer): void |
||
| 140 | { |
||
| 141 | $timeElapsed = number_format($timer->result(), 2, '.', ''); |
||
| 142 | $this->footer->writeln( |
||
| 143 | sprintf( |
||
| 144 | ' <fg=white;options=bold>Time: </><fg=default>%ss</>', |
||
| 145 | $timeElapsed |
||
| 146 | ) |
||
| 147 | ); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Displays the error using Collision's writer |
||
| 152 | * and terminates with exit code === 1. |
||
| 153 | * |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | public function writeError(State $state, Throwable $throwable) |
||
| 157 | { |
||
| 158 | $this->writeCurrentRecap($state); |
||
| 159 | |||
| 160 | $this->updateFooter($state); |
||
| 161 | |||
| 162 | $writer = (new Writer())->setOutput($this->output); |
||
| 163 | |||
| 164 | if ($throwable instanceof AssertionFailedError) { |
||
| 165 | $writer->showTitle(false); |
||
| 166 | $this->output->write('', true); |
||
| 167 | } |
||
| 168 | |||
| 169 | $writer->ignoreFilesIn([ |
||
| 170 | '/vendor\/phpunit\/phpunit\/src/', |
||
| 171 | '/vendor\/mockery\/mockery/', |
||
| 172 | '/vendor\/laravel\/framework\/src\/Illuminate\/Testing/', |
||
| 173 | '/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Testing/', |
||
| 174 | ]); |
||
| 175 | |||
| 176 | if ($throwable instanceof ExceptionWrapper && $throwable->getOriginalException() !== null) { |
||
| 177 | $throwable = $throwable->getOriginalException(); |
||
| 178 | } |
||
| 179 | |||
| 180 | $inspector = new Inspector($throwable); |
||
| 181 | |||
| 182 | $writer->write($inspector); |
||
| 183 | |||
| 184 | if ($throwable instanceof ExpectationFailedException && $comparisionFailure = $throwable->getComparisonFailure()) { |
||
| 185 | $this->output->write($comparisionFailure->getDiff()); |
||
| 186 | } |
||
| 187 | |||
| 188 | exit(1); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Returns the title contents. |
||
| 193 | */ |
||
| 194 | private function titleLineFrom(string $fg, string $bg, string $title, string $testCaseName): string |
||
| 195 | { |
||
| 196 | if (class_exists($testCaseName)) { |
||
| 197 | $nameParts = explode('\\', $testCaseName); |
||
| 198 | $highlightedPart = array_pop($nameParts); |
||
| 199 | $nonHighlightedPart = implode('\\', $nameParts); |
||
| 200 | $testCaseName = sprintf("\e[2m%s\e[22m<fg=white;options=bold>%s</>", "$nonHighlightedPart\\", $highlightedPart); |
||
| 201 | } elseif (file_exists($testCaseName)) { |
||
| 202 | $testCaseName = substr($testCaseName, strlen((string) getcwd()) + 1); |
||
| 203 | $nameParts = explode(DIRECTORY_SEPARATOR, $testCaseName); |
||
| 204 | $highlightedPart = (string) array_pop($nameParts); |
||
| 205 | $highlightedPart = substr($highlightedPart, 0, (int) strrpos($highlightedPart, '.')); |
||
| 206 | $nonHighlightedPart = implode('\\', $nameParts); |
||
| 207 | $testCaseName = sprintf("\e[2m%s\e[22m<fg=white;options=bold>%s</>", "$nonHighlightedPart\\", $highlightedPart); |
||
| 208 | } |
||
| 209 | |||
| 210 | return sprintf( |
||
| 211 | "\n <fg=%s;bg=%s;options=bold> %s </><fg=default> %s</>", |
||
| 212 | $fg, |
||
| 213 | $bg, |
||
| 214 | $title, |
||
| 215 | $testCaseName |
||
| 216 | ); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Returns the test contents. |
||
| 221 | */ |
||
| 222 | private function testLineFrom(string $fg, string $icon, string $description, string $warning = null): string |
||
| 223 | { |
||
| 224 | if (!empty($warning)) { |
||
| 225 | $warning = sprintf( |
||
| 226 | ' → %s', |
||
| 227 | $warning |
||
| 228 | ); |
||
| 229 | } |
||
| 230 | |||
| 231 | return sprintf( |
||
| 232 | " <fg=%s;options=bold>%s</><fg=default> \e[2m%s\e[22m</><fg=yellow>%s</>", |
||
| 233 | $fg, |
||
| 234 | $icon, |
||
| 235 | $description, |
||
| 236 | $warning |
||
| 237 | ); |
||
| 238 | } |
||
| 239 | } |
||
| 240 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.