Skip to content

Commit 97ca437

Browse files
authored
feat: #22 pmd
1 parent b6d8e2b commit 97ca437

8 files changed

+165
-15
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ php artisan lint:publish
2525
php artisan lint:code
2626
php artisan lint:code --fix
2727
php artisan lint:code app/ tests/
28-
php artisan lint:code --standard=Squiz app/
28+
php artisan lint:code app/ tests/ --fix
29+
php artisan lint:phpcs
30+
php artisan lint:pmd
2931
php artisan lint:staged
3032
```
3133

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"squizlabs/php_codesniffer": ">=3.5"
3636
},
3737
"require-dev": {
38-
"orchestra/testbench": ">=v7"
38+
"orchestra/testbench": ">=v7",
39+
"php-mock/php-mock": "^2.5"
3940
}
4041
}

src/LintCodeCommand.php

+7-13
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ class LintCodeCommand extends Command
1515
*/
1616
protected $signature = 'lint:code
1717
{files?*}
18-
{--fix : automatic fix}
19-
{--standard=phpcs.xml : coding standards}';
18+
{--fix : automatic fix}';
2019

2120
/**
2221
* The console command description.
@@ -32,17 +31,12 @@ class LintCodeCommand extends Command
3231
*/
3332
public function handle()
3433
{
35-
$bin = $this->option('fix') ? 'phpcbf' : 'phpcs';
36-
$files = empty($this->argument('files')) ? ['.'] : $this->argument('files');
37-
$command = "vendor" . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "$bin --standard=";
38-
exec(
39-
$command . $this->option('standard') . ' ' . implode(' ', $files),
40-
$output,
41-
$code
42-
);
43-
foreach ($output as $line) {
44-
$this->line($line);
45-
}
34+
$code = $this->call('lint:phpcs', [
35+
'files' => $this->argument('files'), '--fix' => $this->option('fix')
36+
]);
37+
$code += $this->call('lint:phpmd', [
38+
'files' => $this->argument('files')
39+
]);
4640
return $code;
4741
}
4842
}

src/LintPhpcsCommand.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace LaravelFans\Lint;
4+
5+
use FilesystemIterator;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\File;
8+
9+
class LintPhpcsCommand extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'lint:phpcs
17+
{--fix : automatic fix}
18+
{--standard=phpcs.xml : coding standards}
19+
{files?*}';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Lint by phpcs';
27+
28+
/**
29+
* Execute the console command.
30+
*
31+
* @return void
32+
*/
33+
public function handle()
34+
{
35+
$bin = $this->option('fix') ? 'phpcbf' : 'phpcs';
36+
$files = empty($this->argument('files')) ? ['.'] : $this->argument('files');
37+
$command = "vendor" . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "$bin --standard=";
38+
exec(
39+
$command . $this->option('standard') . ' ' . implode(' ', $files),
40+
$output,
41+
$code
42+
);
43+
foreach ($output as $line) {
44+
$this->line($line);
45+
}
46+
return $code;
47+
}
48+
}

src/LintPmdCommand.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace LaravelFans\Lint;
4+
5+
use FilesystemIterator;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\File;
8+
9+
class LintPmdCommand extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'lint:pmd
17+
{files?*}
18+
{--format=text}
19+
{--ruleset=phpmd.xml}';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Lint by phpmd';
27+
28+
/**
29+
* Execute the console command.
30+
*
31+
* @return void
32+
*/
33+
public function handle()
34+
{
35+
$files = empty($this->argument('files')) ? ['.'] : $this->argument('files');
36+
$command = "vendor" . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "phpmd ";
37+
exec(
38+
$command . implode(' ', $files) . ' ' . $this->option('format') . ' ' . $this->option('ruleset'),
39+
$output,
40+
$code
41+
);
42+
foreach ($output as $line) {
43+
$this->line($line);
44+
}
45+
return $code;
46+
}
47+
}

src/LintServiceProvider.php

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public function boot()
1616
if ($this->app->runningInConsole()) {
1717
$this->commands([
1818
LintCodeCommand::class,
19+
LintPmdCommand::class,
20+
LintPhpcsCommand::class,
1921
LintPublishCommand::class,
2022
LintRouteCommand::class,
2123
LintStagedCommand::class,

tests/LintPhpcsCommandTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace LaravelFans\Lint\Tests;
4+
5+
use Illuminate\Support\Facades\File;
6+
use phpmock\MockBuilder;
7+
use phpmock\functions\FixedValueFunction;
8+
9+
class LintPhpcsCommandTest extends TestCase
10+
{
11+
public function testLintPhpcsWithoutArgs()
12+
{
13+
$builder = new MockBuilder();
14+
$builder->setNamespace('\\LaravelFans\\Lint')
15+
->setName("exec")
16+
->setFunction(
17+
function ($command, &$output, &$code) {
18+
$this->assertEquals("vendor/bin/phpcs --standard=phpcs.xml .", $command);
19+
$output = [];
20+
$code = 0;
21+
}
22+
);
23+
$mock = $builder->build();
24+
$mock->enable();
25+
$this->artisan('lint:phpcs')->assertExitCode(0);
26+
$mock->disable();
27+
}
28+
}

tests/LintPmdCommandTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace LaravelFans\Lint\Tests;
4+
5+
use Illuminate\Support\Facades\File;
6+
use phpmock\MockBuilder;
7+
use phpmock\functions\FixedValueFunction;
8+
9+
class LintPmdCommandTest extends TestCase
10+
{
11+
public function testLintPmdWithoutArgs()
12+
{
13+
$builder = new MockBuilder();
14+
$builder->setNamespace('\\LaravelFans\\Lint')
15+
->setName("exec")
16+
->setFunction(
17+
function ($command, &$output, &$code) {
18+
$this->assertEquals("vendor/bin/phpmd . text phpmd.xml", $command);
19+
$output = [];
20+
$code = 1;
21+
}
22+
);
23+
$mock = $builder->build();
24+
$mock->enable();
25+
$this->artisan('lint:pmd')->assertExitCode(1);
26+
$mock->disable();
27+
}
28+
}

0 commit comments

Comments
 (0)