Skip to content

Commit a27a434

Browse files
committed
feat: #16 phpmd
1 parent f0d8c95 commit a27a434

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
},
3030
"minimum-stability": "stable",
3131
"require": {
32+
"ext-json": "*",
3233
"laravel/framework": ">=v6.18.38",
33-
"squizlabs/php_codesniffer": "^3.5",
34-
"ext-json": "*"
34+
"phpmd/phpmd": "^2.10",
35+
"squizlabs/php_codesniffer": "^3.5"
3536
},
3637
"require-dev": {
3738
"orchestra/testbench": ">=v4.8.0"

src/LintPublishCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace LaravelFans\Lint;
44

5-
use FilesystemIterator;
65
use Illuminate\Console\Command;
76
use Illuminate\Support\Facades\File;
87

@@ -32,6 +31,7 @@ public function handle()
3231
$basePath = $this->laravel->basePath();
3332

3433
File::copy(__DIR__ . '/stubs/phpcs.xml', $basePath . '/phpcs.xml');
34+
File::copy(__DIR__ . '/stubs/phpmd.xml', $basePath . '/phpmd.xml');
3535
if (File::exists($basePath . '/.git/hooks')) {
3636
File::copy(__DIR__ . '/stubs/git-pre-commit', $basePath . '/.git/hooks/pre-commit');
3737
File::chmod($basePath . '/.git/hooks/pre-commit', 0755);

src/stubs/git-pre-commit

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/bin/sh
22

3-
files=$(git diff --diff-filter=d --name-only HEAD | grep ".php$")
4-
5-
if [ -n "$files" ] ; then
6-
./vendor/bin/phpcs --standard=phpcs.xml $files
7-
fi
3+
FILES=$(git diff --diff-filter=d --name-only HEAD | { grep '.php$' || true; })
4+
for file in $FILES; do
5+
./vendor/bin/phpcs --extensions=php --standard=PSR12 "$file"
6+
./vendor/bin/phpmd "$file" text phpmd.xml --exclude vendor
7+
done
8+
# XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-text --coverage-filter=app/ tests/

src/stubs/phpmd.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<pmd>
3+
<rule ref="rulesets/cleancode.xml">
4+
<exclude name="StaticAccess" />
5+
</rule>
6+
7+
<rule ref="rulesets/codesize.xml" />
8+
<rule ref="rulesets/controversial.xml" />
9+
<rule ref="rulesets/design.xml" />
10+
11+
<rule ref="rulesets/naming.xml">
12+
<exclude name="ShortVariable" />
13+
</rule>
14+
15+
<rule ref="rulesets/naming.xml/ShortVariable"
16+
since="0.2"
17+
message="Avoid variables with short names like {0}. Configured minimum length is {1}."
18+
class="PHPMD\Rule\Naming\ShortVariable"
19+
externalInfoUrl="http://phpmd.org/rules/naming.html#shortvariable">
20+
<priority>3</priority>
21+
<properties>
22+
<property name="minimum" description="Minimum length for a variable, property or parameter name" value="3"/>
23+
<property name="exceptions" value="id,q,w,i,j,k,v,e,f,fp" />
24+
</properties>
25+
</rule>
26+
27+
<rule ref="rulesets/unusedcode.xml" />
28+
</pmd>

tests/LintPublishCommandTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public function testGitNotExists()
1212
File::deleteDirectory($laravelPath . '/.git/', true);
1313
$this->artisan('lint:publish')->run();
1414
$this->assertFileExists($laravelPath . '/phpcs.xml');
15+
$this->assertFileExists($laravelPath . '/phpmd.xml');
1516
$this->assertFileDoesNotExist($laravelPath . '/.git/hooks/pre-commit');
1617
}
1718

@@ -21,6 +22,7 @@ public function testGitExists()
2122
File::makeDirectory($laravelPath . '/.git/hooks/', 0755, true);
2223
$this->artisan('lint:publish')->run();
2324
$this->assertFileExists($laravelPath . '/phpcs.xml');
25+
$this->assertFileExists($laravelPath . '/phpmd.xml');
2426
$this->assertFileExists($laravelPath . '/.git/hooks/pre-commit');
2527
}
2628
}

0 commit comments

Comments
 (0)