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

feat: #22 lint by pmd #30

Merged
merged 2 commits into from
Nov 17, 2024
Merged
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
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 @@
*/
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 @@
*/
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')
]);

Check warning on line 39 in src/LintCodeCommand.php

View check run for this annotation

Codecov / codecov/patch

src/LintCodeCommand.php#L34-L39

Added lines #L34 - L39 were not covered by tests
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);

Check warning on line 44 in src/LintPhpcsCommand.php

View check run for this annotation

Codecov / codecov/patch

src/LintPhpcsCommand.php#L44

Added line #L44 was not covered by tests
}
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);

Check warning on line 43 in src/LintPmdCommand.php

View check run for this annotation

Codecov / codecov/patch

src/LintPmdCommand.php#L43

Added line #L43 was not covered by tests
}
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();
}
}
Loading