Skip to content

Commit

Permalink
feat (ConfigurationFile) Add php format
Browse files Browse the repository at this point in the history
  • Loading branch information
Wtyd committed Jan 23, 2025
1 parent 699043a commit 2e5dcdd
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 8 deletions.
66 changes: 66 additions & 0 deletions qa/githooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
return [
'Options' => [
'execution' => 'full', // full (default), fast
'processes' => 2,
],
'Tools' => [
'phpstan',
'parallel-lint',
'phpmd',
'phpcpd',
'phpcbf',
'phpcs',
],
'phpstan' => [
'executablePath' => 'vendor/bin/phpstan',
'config' => './qa/phpstan.neon',
// 'memory-limit' => '1G', // Examples: 1M 2000M 1G 5G
'paths' => ['src'],
// 'level' => 8, // level 0-8 (0 default, 8 max)
'otherArguments' => '--no-progress --ansi',
],
'parallel-lint' => [
'executablePath' => 'vendor/bin/parallel-lint',
'paths' => ['./'],
'exclude' => ['vendor', 'qa', 'tools'],
'otherArguments' => '--colors',
// 'ignoreErrorsOnExit' => true,
],
'phpcs' => [
'executablePath' => 'tools/php71/phpcs',
'paths' => ['./'],
'standard' => './qa/psr12-ruleset.xml', // or predefined rules: Squiz, PSR12, Generic, PEAR
'ignore' => ['vendor', 'tools'],
'error-severity' => 1,
'warning-severity' => 6,
'otherArguments' => '--report=summary --parallel=2',
],
'phpcbf' => [
'usePhpcsConfiguration' => true,
// 'executablePath' => 'tools/php71/phpcbf',
// 'paths' => ['./'],
// 'standard' => './qa/psr12-ruleset.xml', // or predefined rules: Squiz, PSR12, Generic, PEAR
// 'ignore' => ['vendor'], // Se podría configurar en el standard directamente
// 'error-severity' => 1,
// 'warning-severity' => 6,
],
'phpmd' => [
'executablePath' => 'tools/php71/phpmd',
'paths' => ['./src/'],
'rules' => './qa/phpmd-ruleset.xml', // or predefined rules cleancode,codesize,controversial,design,naming,unusedcode
'exclude' => ['vendor'], // Se podría configurar en las rules directamente
// 'otherArguments' => '--strict',
// 'ignoreErrorsOnExit' => true,
],
'phpcpd' => [
'executablePath' => 'tools/php71/phpcpd',
'paths' => ['./'],
'exclude' => ['vendor', 'tests', 'tools'],
// 'otherArguments' => '--min-lines=5',
],
'security-checker' => [
'executablePath' => 'tools/php71/local-php-security-checker',
// 'otherArguments' => '-format json',
],
];
39 changes: 31 additions & 8 deletions src/ConfigurationFile/FileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Wtyd\GitHooks\ConfigurationFile;

use InvalidArgumentException;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
use Wtyd\GitHooks\ConfigurationFile\Exception\ConfigurationFileNotFoundException;
Expand All @@ -20,17 +21,29 @@ public function __construct()
}

/**
* @return array File configuration githooks.yml in associative array format.
* @return array File configuration in associative array format.
*
* @throws \Wtyd\GitHooks\ConfigurationFile\Exception\ParseConfigurationFileException
* @throws \Wtyd\GitHooks\ConfigurationFile\Exception\ConfigurationFileNotFoundException
* @throws \InvalidArgumentException
*/
public function readFile(): array
{
$configurationFilePath = $this->findConfigurationFile();

try {
$configurationFile = Yaml::parseFile($configurationFilePath);
$fileExtension = pathinfo($configurationFilePath, PATHINFO_EXTENSION);

if ($fileExtension === 'yml' || $fileExtension === 'yaml') {
$configurationFile = Yaml::parseFile($configurationFilePath);
} elseif ($fileExtension === 'php') {
$configurationFile = require $configurationFilePath;
if (!is_array($configurationFile)) {
throw new ParseConfigurationFileException('PHP configuration file does not return an array.');
}
} else {
throw new InvalidArgumentException('Unsupported file type.');
}
} catch (ParseException $exception) {
throw ParseConfigurationFileException::forMessage($exception->getMessage());
}
Expand All @@ -39,19 +52,29 @@ public function readFile(): array
}

/**
* Searchs configuration file 'githooks.yml' on root path and qa/ directory
* Searchs configuration file on root path and qa/ directory
*
* @return string The path of configuration file
*
* @throws \Wtyd\GitHooks\ConfigurationFile\Exception\ConfigurationFileNotFoundException
*/
public function findConfigurationFile(): string
{
if (file_exists("$this->rootPath/githooks.yml")) {
$configFile = "$this->rootPath/githooks.yml";
} elseif (file_exists("$this->rootPath/qa/githooks.yml")) {
$configFile = "$this->rootPath/qa/githooks.yml";
} else {
$possiblePaths = [
"$this->rootPath/githooks.php",
"$this->rootPath/qa/githooks.php",
"$this->rootPath/githooks.yml",
"$this->rootPath/qa/githooks.yml"
];

foreach ($possiblePaths as $path) {
if (file_exists($path)) {
$configFile = $path;
break;
}
}

if (!isset($configFile)) {
throw new ConfigurationFileNotFoundException();
}

Expand Down

0 comments on commit 2e5dcdd

Please sign in to comment.