Skip to content

Commit

Permalink
Merge pull request #32 from mll-lab/ignores-list-inspection-by-name
Browse files Browse the repository at this point in the history
Allow IgnoreByNameSuppressorInspection to ignore inspections that target lists
  • Loading branch information
owenvoke committed Jul 3, 2024
2 parents 3287ea0 + 6ec138f commit 5d684f7
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 42 deletions.
19 changes: 6 additions & 13 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
includes:
- ./vendor/worksome/coding-style/phpstan.neon

- vendor/worksome/coding-style/phpstan.neon
parameters:
paths:
- src
- tests

- src
- tests
level: 9

ignoreErrors:
- '#Call to an undefined method Pest\\Expectation.*#'
-
message: "#Undefined variable: \\$this#"
path: tests/**
-
message: '#^Instanceof between GraphQL\\Language\\AST\\Node and GraphQL\\Language\\AST\\NodeList will always evaluate to false\.$#'
path: src/Inspections/IgnoreByNameSuppressorInspection.php
- '#Call to an undefined method Pest\\Expectation.*#'
- message: "#Undefined variable: \\$this#"
path: tests/**
3 changes: 2 additions & 1 deletion src/Contracts/SuppressorInspection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
namespace Worksome\Graphlint\Contracts;

use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeList;
use Worksome\Graphlint\Inspections\Inspection;

interface SuppressorInspection
{
/**
* @param Node[] $parents
* @param array<Node|NodeList<Node>> $parents
*/
public function shouldSuppress(Node $node, array $parents, Inspection $inspection): bool;
}
34 changes: 12 additions & 22 deletions src/Inspections/IgnoreByNameSuppressorInspection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Worksome\Graphlint\Inspections;

use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeList;
use Worksome\Graphlint\Contracts\SuppressorInspection;
use Worksome\Graphlint\Utils\NodeNameResolver;

Expand All @@ -23,27 +22,18 @@ public function __construct(

public function shouldSuppress(Node $node, array $parents, Inspection $inspection): bool
{
$name = $this->nameResolver->getName($node);

$parent = end($parents);
if ($parent === false || $parent instanceof NodeList) {
$parentName = null;
} else {
$parentName = $this->nameResolver->getName($parent);
}

if ($name === null) {
return false;
}

// Check if name in names
if (in_array($name, $this->names)) {
return true;
}

// Check if name dotted with parents in names
if (in_array("$parentName.$name", $this->names)) {
return true;
$path = [];
foreach ([...$parents, $node] as $ancestor) {
$name = $this->nameResolver->getName($ancestor);
if ($name === null) {
continue;
}

$path[] = $name;
$fullName = implode('.', $path);
if (in_array($fullName, $this->names)) {
return true;
}
}

return false;
Expand Down
4 changes: 2 additions & 2 deletions src/Inspections/MutationFieldArgumentNamedInputInspection.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public function visitObjectTypeDefinition(
Collection::make($fields)
// Get all arguments of the fields
->flatMap(fn(FieldDefinitionNode $node) => iterator_to_array($node->arguments))
// Filter down to arguments which are not named `input`
->filter(fn(InputValueDefinitionNode $node) => $this->nameResolver->getName($node) !== 'input')
// Reject arguments which are named `input`
->reject(fn(InputValueDefinitionNode $node) => $this->nameResolver->getName($node) === 'input')
// Register a problem on each of the arguments
->each(fn(InputValueDefinitionNode $node) => $problemsHolder->registerProblemWithDescription(
$node->name,
Expand Down
8 changes: 6 additions & 2 deletions src/Utils/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

use GraphQL\Language\AST\NameNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeList;

class NodeNameResolver
{
public function getName(?Node $node): ?string
/**
* @param Node|NodeList<Node>|null $node
*/
public function getName(Node|NodeList|null $node): ?string
{
if ($node === null) {
if (! $node instanceof Node) {
return null;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Visitors/VisitorCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\NodeList;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
use GraphQL\Language\AST\UnionTypeDefinitionNode;
Expand Down Expand Up @@ -144,7 +145,7 @@ private function wrapper(
$key,
$parent,
$path,
$ancestors,
array $ancestors,
) use (
$closure,
$affectedInspections,
Expand All @@ -171,7 +172,7 @@ private function wrapper(
}

/**
* @param Node[] $parent
* @param array<Node|NodeList<Node>> $parent
*/
private function shouldSkip(Node $node, array $parent, Inspection $inspection): bool
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type User {
alwaysTwoItems: [Int]!
}
22 changes: 22 additions & 0 deletions tests/Feature/Inspections/IgnoreByNameSuppressorInspectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Worksome\Graphlint\Tests\Feature\Inspections;

use Symplify\SmartFileSystem\SmartFileInfo;
use Worksome\Graphlint\Inspections\IgnoreByNameSuppressorInspection;

use Worksome\Graphlint\Inspections\NonNullableInsideListInspection;
use function Worksome\Graphlint\Tests\getFixturesForDirectory;

it('ignores list inspection by name', function (SmartFileInfo $smartFileInfo) {
$inspection = $this->app->get(NonNullableInsideListInspection::class);
$suppressor = $this->app->get(IgnoreByNameSuppressorInspection::class);
$suppressor->configure('User.alwaysTwoItems');

expect($smartFileInfo)
->toPassInspection($inspection, $suppressor);
})->with(getFixturesForDirectory(
__DIR__ . '/../../../test-resources/Inspections/IgnoreByNameSuppressorInspectionTest'
));

0 comments on commit 5d684f7

Please sign in to comment.