Skip to content

Commit

Permalink
add php mess detector
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar Cabrera committed Jun 26, 2024
1 parent bcfb98b commit f644000
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 8 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/phpmd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Sample workflow for validating and linting PHP code with PHP Mess Detector
name: PHPMD

on:
push:
branches:
- 'Feature/*'

jobs:
phpstan:
runs-on: ubuntu-24.04

steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Cache Composer dependencies
uses: actions/cache@v2
with:
path: vendor
key: composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
composer-
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
tools: composer

- name: Install dependencies
run: composer install

- name: Run PHPStan
run: composer phpmd
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[![VitePress](https://github.com/oscabrera/model-repository/actions/workflows/deploy.yml/badge.svg)](https://github.com/oscabrera/model-repository/actions/workflows/deploy.yml)
[![PHPStan](https://github.com/oscabrera/model-repository/actions/workflows/phpstan.yml/badge.svg)](https://github.com/oscabrera/model-repository/actions/workflows/phpstan.yml)
[![Pint](https://github.com/oscabrera/model-repository/actions/workflows/pint.yml/badge.svg)](https://github.com/oscabrera/model-repository/actions/workflows/pint.yml)
[![PHPMD](https://github.com/oscabrera/model-repository/actions/workflows/phpmd.yml/badge.svg)](https://github.com/oscabrera/model-repository/actions/workflows/phpmd.yml)

[![built with Codeium](https://codeium.com/badges/main)](https://codeium.com)

Expand Down
8 changes: 4 additions & 4 deletions ci-scripts/Analyzer/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ private function execute(string $modifiedFiles): bool
{
$additionalArgs = $this->getAdditionalArgs();
$this->echoColor(
"$this->command $modifiedFiles " . implode(' ', $additionalArgs),
str_replace('%FILES%', $modifiedFiles, $this->command) . implode(' ', $additionalArgs),
$this->color::get('GREEN'),
$this->icon::get('COMMAND')
);
$command = "$this->command $modifiedFiles " . implode(' ', $additionalArgs);
passthru($command, $return_var);
return $return_var === 0;
$command = str_replace('%FILES%', $modifiedFiles, $this->command). ' ' . implode(' ', $additionalArgs);
passthru($command, $returnVar);
return $returnVar === 0;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ci-scripts/Pint/PintAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(array $args)
{
$this->setArgs($args);
$this->setTool('Pint');
$this->setCommand('./vendor/bin/pint --test --config ci-scripts/Pint/pint.json --ansi');
$this->setCommand('./vendor/bin/pint --test --config ci-scripts/Pint/pint.json --ansi %FILES%');
parent::__construct();
}
}
22 changes: 22 additions & 0 deletions ci-scripts/phpMD/PhpMDAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Oscabrera\ModelRepository\CIScripts\phpMD;

require_once __DIR__ . '/../Analyzer/Analyzer.php';

use Oscabrera\ModelRepository\CIScripts\Analyzer\Analyzer;

class PhpMDAnalyzer extends Analyzer
{
/**
* @param array<int, string> $args
* Constructor for the class, command is for validated in the PHP Mess Detector.
*/
public function __construct(array $args)
{
$this->setArgs($args);
$this->setTool('PHP Mess Detector');
$this->setCommand('./vendor/bin/phpmd %FILES% ansi cleancode,codesize,controversial,design,unusedcode --exclude *vendor');
parent::__construct();
}
}
13 changes: 13 additions & 0 deletions ci-scripts/phpMD/run.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Oscabrera\ModelRepository\CIScripts;

require_once __DIR__ . '/PhpMDAnalyzer.php';

use Oscabrera\ModelRepository\CIScripts\phpMD\PhpMDAnalyzer as Analyzer;

/**
* Analyze with PHP Mess Detector
*/
$args = array_slice($_SERVER['argv'], 1); // Get arguments passed to the script
exit((new Analyzer($args))->analyze() ? 0 : 1);
2 changes: 1 addition & 1 deletion ci-scripts/phpStan/PhpStanAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PhpStanAnalyzer extends Analyzer
public function __construct(array $args)
{
$this->setArgs($args);
$this->setCommand('./vendor/bin/phpstan analyse --memory-limit=1G -c ci-scripts/phpStan/phpstan.neon --ansi');
$this->setCommand('./vendor/bin/phpstan analyse --memory-limit=1G -c ci-scripts/phpStan/phpstan.neon --ansi %FILES%');
parent::__construct();
}
}
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
],
"require-dev": {
"phpstan/phpstan": "^1.11",
"laravel/pint": "^1.16"
"laravel/pint": "^1.16",
"phpmd/phpmd": "^2.15"
},
"scripts": {
"phpstan": "php ci-scripts/phpStan/run.php",
"pint": "php ci-scripts/Pint/run.php"
"pint": "php ci-scripts/Pint/run.php",
"phpmd": "php ci-scripts/phpMD/run.php"
}
}
2 changes: 2 additions & 0 deletions docs/getting-started/Introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

[![Pint](https://github.com/oscabrera/model-repository/actions/workflows/pint.yml/badge.svg)](https://github.com/oscabrera/model-repository/actions/workflows/pint.yml)

[![PHPMD](https://github.com/oscabrera/model-repository/actions/workflows/phpmd.yml/badge.svg)](https://github.com/oscabrera/model-repository/actions/workflows/phpmd.yml)

![model-repository](https://socialify.git.ci/Oscabrera/model-repository/image?language=1&name=1&owner=1&pattern=Floating%20Cogs&theme=Auto)

This package for Laravel greatly simplifies the process of creating a complete RESTful API for any model in your
Expand Down

0 comments on commit f644000

Please sign in to comment.