From dcf76c1a81cfa916c885bf03645c941121007353 Mon Sep 17 00:00:00 2001 From: mzglinski Date: Tue, 21 Apr 2020 21:36:27 +0200 Subject: [PATCH] Generate noinspections PHPStorm tags (#905) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Generate noinspections PHPStorm tags * Add comments about PHPStorm noinspection tags * Fix line to long warnings * Add tests for PHPStorm noinspection Co-authored-by: Michał Zgliński --- src/Console/ModelsCommand.php | 20 +++ .../PHPStormNoInspection/Models/Simple.php | 9 ++ .../PHPStormNoInspection/Test.php | 136 ++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 tests/Console/ModelsCommand/PHPStormNoInspection/Models/Simple.php create mode 100644 tests/Console/ModelsCommand/PHPStormNoInspection/Test.php diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 7a00418..b08b237 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -59,6 +59,7 @@ class ModelsCommand extends Command protected $dirs = array(); protected $reset; protected $keep_text; + protected $phpstorm_noinspections; /** * @var bool[string] */ @@ -97,6 +98,7 @@ class ModelsCommand extends Command $model = $this->argument('model'); $ignore = $this->option('ignore'); $this->reset = $this->option('reset'); + $this->phpstorm_noinspections = $this->option('phpstorm-noinspections'); if ($this->option('smart-reset')) { $this->keep_text = $this->reset = true; } @@ -155,6 +157,10 @@ class ModelsCommand extends Command array('nowrite', 'N', InputOption::VALUE_NONE, 'Don\'t write to Model file'), array('reset', 'R', InputOption::VALUE_NONE, 'Remove the original phpdocs instead of appending'), array('smart-reset', 'r', InputOption::VALUE_NONE, 'Refresh the properties/methods list, but keep the text'), + array('phpstorm-noinspections', 'p', InputOption::VALUE_NONE, + 'Add PhpFullyQualifiedNameUsageInspection and PhpUnnecessaryFullyQualifiedNameInspection PHPStorm ' . + 'noinspection tags' + ), array('ignore', 'I', InputOption::VALUE_OPTIONAL, 'Which models to ignore', ''), ); } @@ -728,6 +734,20 @@ class ModelsCommand extends Command if ($this->write && ! $phpdoc->getTagsByName('mixin')) { $phpdoc->appendTag(Tag::createInstance("@mixin \\Eloquent", $phpdoc)); } + if ($this->phpstorm_noinspections) { + /** + * Facades, Eloquent API + * @see https://www.jetbrains.com/help/phpstorm/php-fully-qualified-name-usage.html + */ + $phpdoc->appendTag(Tag::createInstance("@noinspection PhpFullyQualifiedNameUsageInspection", $phpdoc)); + /** + * Relations, other models in the same namespace + * @see https://www.jetbrains.com/help/phpstorm/php-unnecessary-fully-qualified-name.html + */ + $phpdoc->appendTag( + Tag::createInstance("@noinspection PhpUnnecessaryFullyQualifiedNameInspection", $phpdoc) + ); + } $serializer = new DocBlockSerializer(); $serializer->getDocComment($phpdoc); diff --git a/tests/Console/ModelsCommand/PHPStormNoInspection/Models/Simple.php b/tests/Console/ModelsCommand/PHPStormNoInspection/Models/Simple.php new file mode 100644 index 0000000..6a9f610 --- /dev/null +++ b/tests/Console/ModelsCommand/PHPStormNoInspection/Models/Simple.php @@ -0,0 +1,9 @@ +set('ide-helper', [ + 'model_locations' => [ + // This is calculated from the base_path() which points to + // vendor/orchestra/testbench-core/laravel + '/../../../../tests/Console/ModelsCommand/PHPStormNoInspection/Models', + ], + ]); + } + + public function testNoinspectionNotPresent(): void + { + $actualContent = null; + $mockFilesystem = Mockery::mock(Filesystem::class); + $mockFilesystem + ->shouldReceive('get') + ->andReturn(file_get_contents(__DIR__ . '/Models/Simple.php')) + ->once(); + $mockFilesystem + ->shouldReceive('put') + ->with( + Mockery::any(), + Mockery::capture($actualContent) + ) + ->andReturn(1) // Simulate we wrote _something_ to the file + ->once(); + + $this->instance(Filesystem::class, $mockFilesystem); + + $command = $this->app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, [ + '--write' => true, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertEmpty($tester->getDisplay()); + + $expectedContent = <<<'PHP' +assertSame($expectedContent, $actualContent); + } + + public function testNoinspectionPresent(): void + { + $actualContent = null; + $mockFilesystem = Mockery::mock(Filesystem::class); + $mockFilesystem + ->shouldReceive('get') + ->andReturn(file_get_contents(__DIR__ . '/Models/Simple.php')) + ->once(); + $mockFilesystem + ->shouldReceive('put') + ->with( + Mockery::any(), + Mockery::capture($actualContent) + ) + ->andReturn(1) // Simulate we wrote _something_ to the file + ->once(); + + $this->instance(Filesystem::class, $mockFilesystem); + + $command = $this->app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, [ + '--write' => true, + '--phpstorm-noinspections' => true, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertEmpty($tester->getDisplay()); + + $expectedContent = <<<'PHP' +assertSame($expectedContent, $actualContent); + } +}