-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only chek if error starts with a text to avoid issues when the last p…
…art change based on cakephp version.
- Loading branch information
1 parent
54c2a17
commit 83b3879
Showing
3 changed files
with
136 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace CakeDC\PHPStan\Constraint; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
|
||
class ArrayOfStringStartsWith extends Constraint | ||
{ | ||
/** | ||
* @var array<string> | ||
*/ | ||
private readonly array $actual; | ||
/** | ||
* @var array<array{expected: string, type: string, actual: string|null}> | ||
*/ | ||
private array $result = []; | ||
/** | ||
* @var array<string> | ||
*/ | ||
private array $notExpected = []; | ||
|
||
/** | ||
* @param array<string> $actual | ||
*/ | ||
public function __construct(array $actual) | ||
{ | ||
$this->actual = $actual; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function toString(): string | ||
{ | ||
return 'a list of errors'; | ||
} | ||
|
||
/** | ||
* @param mixed $other | ||
* @return bool | ||
*/ | ||
protected function matches(mixed $other): bool | ||
{ | ||
$result = true; | ||
$this->notExpected = $this->actual; | ||
assert(is_array($other)); | ||
foreach ($other as $key => $error) { | ||
if (!isset($this->actual[$key])) { | ||
$this->result[$key] = ['expected' => $error, 'type' => 'missing', 'actual' => null]; | ||
$result = false; | ||
continue; | ||
} | ||
unset($this->notExpected[$key]); | ||
if (!str_starts_with($this->actual[$key], $error)) { | ||
$this->result[$key] = ['expected' => $error, 'type' => 'not-equal', 'actual' => $this->actual[$key]]; | ||
$result = false; | ||
} | ||
} | ||
|
||
return $result && empty($this->notExpected); | ||
} | ||
|
||
/** | ||
* @param mixed $other | ||
* @return string | ||
*/ | ||
protected function failureDescription(mixed $other): string | ||
{ | ||
$text = "\n"; | ||
foreach ($this->result as $item) { | ||
if ($item['type'] === 'not-equal') { | ||
$text .= sprintf(" -%s \n +%s \n", $item['expected'], $item['actual']); | ||
} | ||
if ($item['type'] === 'missing') { | ||
$text .= sprintf(" -%s \n", $item['expected']); | ||
} | ||
} | ||
|
||
foreach ($this->notExpected as $item) { | ||
$text .= sprintf(" \n +%s", $item); | ||
} | ||
|
||
return $text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace CakeDC\PHPStan\Rule\Traits; | ||
|
||
use CakeDC\PHPStan\Constraint\ArrayOfStringStartsWith; | ||
use PHPStan\Analyser\Error; | ||
|
||
/** | ||
* @mixin \PHPStan\Testing\RuleTestCase; | ||
*/ | ||
trait AnalyseCheckLineStartsWithTrait | ||
{ | ||
/** | ||
* @param array $files | ||
* @param array $expected | ||
* @return void | ||
*/ | ||
public function analyseCheckLineStartsWith(array $files, array $expected): void | ||
{ | ||
$actualErrors = $this->gatherAnalyserErrors($files); | ||
$messageText = static function (int $line, string $message): string { | ||
return sprintf('%02d: %s', $line, $message); | ||
}; | ||
$actualErrors = array_map(static function (Error $error) use ($messageText): string { | ||
$line = $error->getLine(); | ||
if ($line === null) { | ||
return $messageText(-1, $error->getMessage()); | ||
} | ||
|
||
return $messageText($line, $error->getMessage()); | ||
}, $actualErrors); | ||
|
||
$expected = array_map(static function (array $item) use ($messageText): string { | ||
return $messageText($item[1], $item[0]); | ||
}, $expected); | ||
$this->assertThat($expected, new ArrayOfStringStartsWith($actualErrors)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters