Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add severity to Psalm and PHP_CodeSniffer #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/PHP_CodeSniffer/PhpCodeSnifferConvertToSubset.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function convertToSubset(): void
$filename,
$node->line
),
'severity' => $this->getSeverity($node->type),
'location' => [
'path' => $filename,
'lines' => [
Expand All @@ -41,4 +42,10 @@ public function getToolName(): string
{
return 'PHP_CodeSniffer';
}

private function getSeverity(string $type): string
{
// can be info, minor, major, critical, or blocker
return 'ERROR' === $type ? 'major' : 'minor';
}
}
4 changes: 4 additions & 0 deletions src/PHP_CodeSniffer/PhpCodeSnifferJsonValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function validateJson(): void
if (!property_exists($node, 'line')) {
throw new InvalidJsonException('The [files.messages.line] is a required property');
}

if (!property_exists($node, 'type')) {
throw new InvalidJsonException('The [files.messages.type] is a required property');
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Psalm/PsalmConvertToSubset.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function convertToSubset(): void
$node->file_name,
$node->line_from
),
'severity' => $this->getSeverity($node->severity),
'location' => [
'path' => $node->file_name,
'lines' => [
Expand All @@ -40,4 +41,10 @@ public function getToolName(): string
{
return 'Psalm';
}

private function getSeverity(string $severity): string
{
// can be info, minor, major, critical, or blocker
return 'error' === $severity ? 'major' : 'minor';
}
}
4 changes: 4 additions & 0 deletions src/Psalm/PsalmJsonValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public function validateJson(): void
if (!property_exists($node, 'line_to')) {
throw new InvalidJsonException('The [line_to] is a required property');
}

if (!property_exists($node, 'severity')) {
throw new InvalidJsonException('The [severity] is a required property');
}
}
}
}
3 changes: 3 additions & 0 deletions tests/PHP_CodeSniffer/PhpCodeSnifferConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use BeechIt\JsonToCodeClimateSubsetConverter\Factories\ConverterFactory;
use BeechIt\JsonToCodeClimateSubsetConverter\Factories\ValidatorFactory;
use BeechIt\JsonToCodeClimateSubsetConverter\Tests\TestCase;
use function file_get_contents;
use function json_decode;

/**
* @internal
Expand Down Expand Up @@ -40,6 +42,7 @@ public function testItCanConvertPhpCodeSnifferJsonToSubset(): void
[
'description' => '(PHP_CodeSniffer) Missing file doc comment',
'fingerprint' => 'fa33b2f8044e0f23de6b53f15d4d7bc9',
'severity' => 'major',
'location' => [
'path' => 'app/Class.php',
'lines' => [
Expand Down
35 changes: 31 additions & 4 deletions tests/PHP_CodeSniffer/PhpCodeSnifferValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use BeechIt\JsonToCodeClimateSubsetConverter\Factories\ConverterFactory;
use BeechIt\JsonToCodeClimateSubsetConverter\Factories\ValidatorFactory;
use BeechIt\JsonToCodeClimateSubsetConverter\Tests\TestCase;
use function file_get_contents;
use function json_decode;

/**
* @internal
*/
class PhpCodeSnifferValidationTest extends TestCase
{
public function testItThrowsAnExceptionWhenFilesPropertyIsMissing()
public function testItThrowsAnExceptionWhenFilesPropertyIsMissing(): void
{
$this->expectException(InvalidJsonException::class);
$this->expectErrorMessage('The [files] is a required property');
Expand All @@ -39,7 +41,7 @@ public function testItThrowsAnExceptionWhenFilesPropertyIsMissing()
$converterImplementation->convertToSubset();
}

public function testItThrowsAnExceptionWhenFilesMessagesPropertyIsMissing()
public function testItThrowsAnExceptionWhenFilesMessagesPropertyIsMissing(): void
{
$this->expectException(InvalidJsonException::class);
$this->expectErrorMessage('The [files.messages] is a required property');
Expand All @@ -64,7 +66,7 @@ public function testItThrowsAnExceptionWhenFilesMessagesPropertyIsMissing()
$converterImplementation->convertToSubset();
}

public function testItThrowsAnExceptionWhenFilesMessagesMessagePropertyIsMissing()
public function testItThrowsAnExceptionWhenFilesMessagesMessagePropertyIsMissing(): void
{
$this->expectException(InvalidJsonException::class);
$this->expectErrorMessage('The [files.messages.message] is a required property');
Expand All @@ -89,7 +91,7 @@ public function testItThrowsAnExceptionWhenFilesMessagesMessagePropertyIsMissing
$converterImplementation->convertToSubset();
}

public function testItThrowsAnExceptionWhenFilesMessagesLinePropertyIsMissing()
public function testItThrowsAnExceptionWhenFilesMessagesLinePropertyIsMissing(): void
{
$this->expectException(InvalidJsonException::class);
$this->expectErrorMessage('The [files.messages.line] is a required property');
Expand All @@ -113,4 +115,29 @@ public function testItThrowsAnExceptionWhenFilesMessagesLinePropertyIsMissing()
// When
$converterImplementation->convertToSubset();
}

public function testItThrowsAnExceptionWhenFilesTypePropertyIsMissing(): void
{
$this->expectException(InvalidJsonException::class);
$this->expectErrorMessage('The [files.messages.type] is a required property');

// Given
$jsonInput = file_get_contents(__DIR__.'/fixtures/invalid-files-messages-type-input.json');
$jsonDecodedInput = json_decode($jsonInput);

$validatorFactory = new ValidatorFactory();

$validator = $validatorFactory->build('PHP_CodeSniffer', $jsonDecodedInput);

$converterFactory = new ConverterFactory();

$converterImplementation = $converterFactory->build(
'PHP_CodeSniffer',
$validator,
$jsonDecodedInput
);

// When
$converterImplementation->convertToSubset();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"totals": {
"errors": 1,
"warnings": 0,
"fixable": 0
},
"files": {
"app\/Class.php": {
"errors": 1,
"warnings": 0,
"messages": [
{
"message": "Missing file doc comment",
"source": "PEAR.Commenting.FileComment.Missing",
"severity": 5,
"fixable": false,
"line": 2,
"column": 1
}
]
}
}
}
1 change: 1 addition & 0 deletions tests/PHP_CodeSniffer/fixtures/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
"description": "(PHP_CodeSniffer) Missing file doc comment",
"fingerprint": "fa33b2f8044e0f23de6b53f15d4d7bc9",
"severity": "major",
"location": {
"path": "app/Class.php",
"lines": {
Expand Down
3 changes: 3 additions & 0 deletions tests/Psalm/PsalmConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use BeechIt\JsonToCodeClimateSubsetConverter\Factories\ConverterFactory;
use BeechIt\JsonToCodeClimateSubsetConverter\Factories\ValidatorFactory;
use BeechIt\JsonToCodeClimateSubsetConverter\Tests\TestCase;
use function file_get_contents;
use function json_decode;

/**
* @internal
Expand Down Expand Up @@ -40,6 +42,7 @@ public function testItCanConvertPsalmJsonToSubset(): void
[
'description' => '(Psalm) Property Illuminate\\Foundation\\Console\\Kernel::$artisan is not defined in constructor of App\\Console\\Kernel and in any methods called in the constructor',
'fingerprint' => '206df1cdb86fc7fc14b049a658832473',
'severity' => 'minor',
'location' => [
'path' => 'app/Class.php',
'lines' => [
Expand Down
35 changes: 31 additions & 4 deletions tests/Psalm/PsalmValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use BeechIt\JsonToCodeClimateSubsetConverter\Factories\ConverterFactory;
use BeechIt\JsonToCodeClimateSubsetConverter\Factories\ValidatorFactory;
use BeechIt\JsonToCodeClimateSubsetConverter\Tests\TestCase;
use function file_get_contents;
use function json_decode;

/**
* @internal
*/
class PsalmValidationTest extends TestCase
{
public function testItThrowsAnExceptionWhenDescriptionPropertyIsMissing()
public function testItThrowsAnExceptionWhenDescriptionPropertyIsMissing(): void
{
$this->expectException(InvalidJsonException::class);
$this->expectErrorMessage('The [message] is a required property');
Expand All @@ -39,7 +41,7 @@ public function testItThrowsAnExceptionWhenDescriptionPropertyIsMissing()
$converterImplementation->convertToSubset();
}

public function testItThrowsAnExceptionWhenFileNamePropertyIsMissing()
public function testItThrowsAnExceptionWhenFileNamePropertyIsMissing(): void
{
$this->expectException(InvalidJsonException::class);
$this->expectErrorMessage('The [file_name] is a required property');
Expand All @@ -64,7 +66,7 @@ public function testItThrowsAnExceptionWhenFileNamePropertyIsMissing()
$converterImplementation->convertToSubset();
}

public function testItThrowsAnExceptionWhenLineFromPropertyIsMissing()
public function testItThrowsAnExceptionWhenLineFromPropertyIsMissing(): void
{
$this->expectException(InvalidJsonException::class);
$this->expectErrorMessage('The [line_from] is a required property');
Expand All @@ -89,7 +91,7 @@ public function testItThrowsAnExceptionWhenLineFromPropertyIsMissing()
$converterImplementation->convertToSubset();
}

public function testItThrowsAnExceptionWhenLineToPropertyIsMissing()
public function testItThrowsAnExceptionWhenLineToPropertyIsMissing(): void
{
$this->expectException(InvalidJsonException::class);
$this->expectErrorMessage('The [line_to] is a required property');
Expand All @@ -113,4 +115,29 @@ public function testItThrowsAnExceptionWhenLineToPropertyIsMissing()
// When
$converterImplementation->convertToSubset();
}

public function testItThrowsAnExceptionWhenSeverityPropertyIsMissing(): void
{
$this->expectException(InvalidJsonException::class);
$this->expectErrorMessage('The [severity] is a required property');

// Given
$jsonInput = file_get_contents(__DIR__.'/fixtures/invalid-severity-input.json');
$jsonDecodedInput = json_decode($jsonInput);

$validatorFactory = new ValidatorFactory();

$validator = $validatorFactory->build('Psalm', $jsonDecodedInput);

$converterFactory = new ConverterFactory();

$converterImplementation = $converterFactory->build(
'Psalm',
$validator,
$jsonDecodedInput
);

// When
$converterImplementation->convertToSubset();
}
}
18 changes: 18 additions & 0 deletions tests/Psalm/fixtures/invalid-severity-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"line_from": 2,
"line_to": 2,
"type": "PropertyNotSetInConstructor",
"message": "Property Illuminate\\Foundation\\Console\\Kernel::$artisan is not defined in constructor of App\\Console\\Kernel and in any methods called in the constructor",
"file_name": "app\/Class.php",
"file_path": "\/var\/www\/html\/app\/Console\/Kernel.php",
"snippet": "class Kernel extends ConsoleKernel",
"selected_text": "Kernel",
"from": 141,
"to": 147,
"snippet_from": 135,
"snippet_to": 169,
"column_from": 7,
"column_to": 13
}
]
1 change: 1 addition & 0 deletions tests/Psalm/fixtures/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
"description": "(Psalm) Property Illuminate\\Foundation\\Console\\Kernel::$artisan is not defined in constructor of App\\Console\\Kernel and in any methods called in the constructor",
"fingerprint": "206df1cdb86fc7fc14b049a658832473",
"severity": "minor",
"location": {
"path": "app/Class.php",
"lines": {
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function multipleConvertersProvider(): array
'output' => [
'description' => '(PHP_CodeSniffer) Missing file doc comment',
'fingerprint' => 'fa33b2f8044e0f23de6b53f15d4d7bc9',
'severity' => 'major',
'location' => [
'path' => 'app/Class.php',
'lines' => [
Expand Down Expand Up @@ -103,6 +104,7 @@ public function multipleConvertersProvider(): array
'output' => [
'description' => '(Psalm) Property Illuminate\\Foundation\\Console\\Kernel::$artisan is not defined in constructor of App\\Console\\Kernel and in any methods called in the constructor',
'fingerprint' => '206df1cdb86fc7fc14b049a658832473',
'severity' => 'minor',
'location' => [
'path' => 'app/Class.php',
'lines' => [
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
{
"description": "(PHP_CodeSniffer) Missing file doc comment",
"fingerprint": "fa33b2f8044e0f23de6b53f15d4d7bc9",
"severity": "major",
"location": {
"path": "app/Class.php",
"lines": {
Expand Down Expand Up @@ -43,6 +44,7 @@
{
"description": "(Psalm) Property Illuminate\\Foundation\\Console\\Kernel::$artisan is not defined in constructor of App\\Console\\Kernel and in any methods called in the constructor",
"fingerprint": "206df1cdb86fc7fc14b049a658832473",
"severity": "minor",
"location": {
"path": "app/Class.php",
"lines": {
Expand Down