Skip to content

Commit

Permalink
SUPESC-849 Fixed PHPStan issue (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
olhalivitchuk authored Jun 4, 2024
1 parent ee3c99f commit 9318473
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ protected function fetchNewFileError(string $file, array $message): ?FileErrorDt
$this->assertArrayKeyExists($message, 'line');
$this->assertArrayKeyExists($message, 'message');

$fileError = new FileErrorDto($file, $message['line'], $message['message']);
$fileError = new FileErrorDto($file, (int)$message['line'], (string)$message['message']);

if ($this->baselineStorage->hasFileError($fileError)) {
return null;
}

$this->baselineStorage->addFileError($fileError);

return new FileErrorDto($file, $message['line'], $message['message']);
return new FileErrorDto($file, (int)$message['line'], (string)$message['message']);
}

/**
Expand Down Expand Up @@ -212,7 +212,7 @@ public function reset(): void
*/
protected function assertArrayKeyExists(array $data, string $key, bool $isArray = false): void
{
if (!isset($data[$key]) || ($isArray && !is_array($data[$key]))) {
if (!array_key_exists($key, $data) || ($isArray && !is_array($data[$key]))) {
throw new InvalidArgumentException(sprintf(
'Unable to find %s key or it\'s not an array in %s. Tooling export format is changes.',
$key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,16 @@ public function testFetchProjectFileErrorsAndSaveInBaseLineShouldReturnEmptyWhen
}

/**
* @dataProvider projectFileErrorsDataProvider
*
* @param array<mixed> $toolOutput
* @param array<mixed> $expectedResult
*
* @return void
*/
public function testFetchProjectFileErrorsAndSaveInBaseLineShouldFetchFileErrors(): void
public function testFetchProjectFileErrorsAndSaveInBaseLineShouldFetchFileErrors(array $toolOutput, array $expectedResult): void
{
// Arrange
$toolOutput = ['files' => ['src/someClass.php' => ['messages' => [['line' => 1, 'message' => 'test message']]]]];

$baseLineStorage = new BaselineStorage();
$fileErrorsFetcher = new FileErrorsFetcher('', '', $this->createProcessRunnerServiceMock($toolOutput), $baseLineStorage, $this->createLoggerMock());

Expand All @@ -89,9 +92,30 @@ public function testFetchProjectFileErrorsAndSaveInBaseLineShouldFetchFileErrors

// Assert
$this->assertCount(1, $fileErrors);
$this->assertSame('src/someClass.php', $fileErrors[0]->getFilename());
$this->assertSame(1, $fileErrors[0]->getLine());
$this->assertSame('test message', $fileErrors[0]->getMessage());
$this->assertSame($expectedResult['expectedClass'], $fileErrors[0]->getFilename());
$this->assertSame($expectedResult['expectedLine'], $fileErrors[0]->getLine());
$this->assertSame($expectedResult['expectedMessage'], $fileErrors[0]->getMessage());
}

/**
* @return array<mixed>
*/
public function projectFileErrorsDataProvider(): array
{
return [
[
['files' => ['src/someClass.php' => ['messages' => [['line' => 1, 'message' => 'test message']]]]],
['expectedClass' => 'src/someClass.php', 'expectedLine' => 1, 'expectedMessage' => 'test message'],
],
[
['files' => ['src/someClass.php' => ['messages' => [['line' => null, 'message' => 'test message']]]]],
['expectedClass' => 'src/someClass.php', 'expectedLine' => 0, 'expectedMessage' => 'test message'],
],
[
['files' => ['src/someClass.php' => ['messages' => [['line' => 1, 'message' => null]]]]],
['expectedClass' => 'src/someClass.php', 'expectedLine' => 1, 'expectedMessage' => ''],
],
];
}

/**
Expand Down

0 comments on commit 9318473

Please sign in to comment.