Skip to content

Commit

Permalink
feat: #22 pmd
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkcup authored Nov 17, 2024
1 parent b6d8e2b commit 97ca437
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 15 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ php artisan lint:publish
php artisan lint:code
php artisan lint:code --fix
php artisan lint:code app/ tests/
php artisan lint:code --standard=Squiz app/
php artisan lint:code app/ tests/ --fix
php artisan lint:phpcs
php artisan lint:pmd
php artisan lint:staged
```

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"squizlabs/php_codesniffer": ">=3.5"
},
"require-dev": {
"orchestra/testbench": ">=v7"
"orchestra/testbench": ">=v7",
"php-mock/php-mock": "^2.5"
}
}
20 changes: 7 additions & 13 deletions src/LintCodeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class LintCodeCommand extends Command
*/
protected $signature = 'lint:code
{files?*}
{--fix : automatic fix}
{--standard=phpcs.xml : coding standards}';
{--fix : automatic fix}';

/**
* The console command description.
Expand All @@ -32,17 +31,12 @@ class LintCodeCommand extends Command
*/
public function handle()
{
$bin = $this->option('fix') ? 'phpcbf' : 'phpcs';
$files = empty($this->argument('files')) ? ['.'] : $this->argument('files');
$command = "vendor" . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "$bin --standard=";
exec(
$command . $this->option('standard') . ' ' . implode(' ', $files),
$output,
$code
);
foreach ($output as $line) {
$this->line($line);
}
$code = $this->call('lint:phpcs', [
'files' => $this->argument('files'), '--fix' => $this->option('fix')
]);
$code += $this->call('lint:phpmd', [
'files' => $this->argument('files')
]);
return $code;
}
}
48 changes: 48 additions & 0 deletions src/LintPhpcsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace LaravelFans\Lint;

use FilesystemIterator;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class LintPhpcsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'lint:phpcs
{--fix : automatic fix}
{--standard=phpcs.xml : coding standards}
{files?*}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Lint by phpcs';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$bin = $this->option('fix') ? 'phpcbf' : 'phpcs';
$files = empty($this->argument('files')) ? ['.'] : $this->argument('files');
$command = "vendor" . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "$bin --standard=";
exec(
$command . $this->option('standard') . ' ' . implode(' ', $files),
$output,
$code
);
foreach ($output as $line) {
$this->line($line);
}
return $code;
}
}
47 changes: 47 additions & 0 deletions src/LintPmdCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace LaravelFans\Lint;

use FilesystemIterator;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class LintPmdCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'lint:pmd
{files?*}
{--format=text}
{--ruleset=phpmd.xml}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Lint by phpmd';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$files = empty($this->argument('files')) ? ['.'] : $this->argument('files');
$command = "vendor" . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "phpmd ";
exec(
$command . implode(' ', $files) . ' ' . $this->option('format') . ' ' . $this->option('ruleset'),
$output,
$code
);
foreach ($output as $line) {
$this->line($line);
}
return $code;
}
}
2 changes: 2 additions & 0 deletions src/LintServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public function boot()
if ($this->app->runningInConsole()) {
$this->commands([
LintCodeCommand::class,
LintPmdCommand::class,
LintPhpcsCommand::class,
LintPublishCommand::class,
LintRouteCommand::class,
LintStagedCommand::class,
Expand Down
28 changes: 28 additions & 0 deletions tests/LintPhpcsCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace LaravelFans\Lint\Tests;

use Illuminate\Support\Facades\File;
use phpmock\MockBuilder;
use phpmock\functions\FixedValueFunction;

class LintPhpcsCommandTest extends TestCase
{
public function testLintPhpcsWithoutArgs()
{
$builder = new MockBuilder();
$builder->setNamespace('\\LaravelFans\\Lint')
->setName("exec")
->setFunction(
function ($command, &$output, &$code) {
$this->assertEquals("vendor/bin/phpcs --standard=phpcs.xml .", $command);
$output = [];
$code = 0;
}
);
$mock = $builder->build();
$mock->enable();
$this->artisan('lint:phpcs')->assertExitCode(0);
$mock->disable();
}
}
28 changes: 28 additions & 0 deletions tests/LintPmdCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace LaravelFans\Lint\Tests;

use Illuminate\Support\Facades\File;
use phpmock\MockBuilder;
use phpmock\functions\FixedValueFunction;

class LintPmdCommandTest extends TestCase
{
public function testLintPmdWithoutArgs()
{
$builder = new MockBuilder();
$builder->setNamespace('\\LaravelFans\\Lint')
->setName("exec")
->setFunction(
function ($command, &$output, &$code) {
$this->assertEquals("vendor/bin/phpmd . text phpmd.xml", $command);
$output = [];
$code = 1;
}
);
$mock = $builder->build();
$mock->enable();
$this->artisan('lint:pmd')->assertExitCode(1);
$mock->disable();
}
}

0 comments on commit 97ca437

Please sign in to comment.