Skip to content

Commit 4f4c3d6

Browse files
committed
feat (Configuration File) Add format in php which has priority over yml
1 parent 699043a commit 4f4c3d6

10 files changed

+276
-19
lines changed

.github/workflows/schedule-ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: Schedule CI
22
# https://github.com/shivammathur/setup-php
33
on:
4+
# push
45
schedule:
56
- cron: '0 4 * * sun' # Sundays at 04 am
67

app/Commands/ExecuteToolCommand.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ class ExecuteToolCommand extends BaseCommand
1515
protected $signature = 'tool
1616
{tool : Tool will be run}
1717
{execution? : Override the execution mode of githooks.yml. Values: "fast" and "full"}
18-
{--ignoreErrorsOnExit= : Avoids exit error even if the tool finds some trobule. When tool is \'all\' applies for all tools}
18+
{--ignoreErrorsOnExit= : Avoids exit error even if the tool finds some trouble. When tool is \'all\' applies for all tools}
1919
{--otherArguments= : Other tool options not supported by GitHooks}
2020
{--executablePath= : Path to executable}
2121
{--paths= : Paths or files against the tool will be executed}
22-
{--processes= : Number of parallel processes in which the tools will be executed}';
22+
{--processes= : Number of parallel processes in which the tools will be executed}
23+
{-c|--config= : Path to configuration file}'
24+
;
2325

2426
protected $description = 'Run the tool passed as argument. It must be a supported tool by GitHooks. the available options depend on the tool passed as parameter';
2527

@@ -38,7 +40,8 @@ public function handle()
3840
strval($this->option('otherArguments')),
3941
strval($this->option('executablePath')),
4042
strval($this->option('paths')),
41-
intval($this->option('processes'))
43+
intval($this->option('processes')),
44+
strval($this->option('config'))
4245
));
4346

4447
$tools = $this->toolsPreparer->__invoke($configurationFile);

qa/githooks.dist.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Old Configuration File. Now in php format
12
Options:
23
execution: full # Optional: default full. Values: full or fast
34
processes: 1 # Optional: default 1. Number of parallel processes

qa/githooks.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
return [
3+
'Options' => [
4+
'execution' => 'full', // full (default), fast
5+
'processes' => 2,
6+
],
7+
'Tools' => [
8+
'phpstan',
9+
'parallel-lint',
10+
'phpmd',
11+
'phpcpd',
12+
'phpcbf',
13+
'phpcs',
14+
],
15+
'phpstan' => [
16+
'executablePath' => 'vendor/bin/phpstan',
17+
'config' => './qa/phpstan.neon',
18+
// 'memory-limit' => '1G', // Examples: 1M 2000M 1G 5G
19+
'paths' => ['src'],
20+
// 'level' => 8, // level 0-8 (0 default, 8 max)
21+
'otherArguments' => '--no-progress --ansi',
22+
],
23+
'parallel-lint' => [
24+
'executablePath' => 'vendor/bin/parallel-lint',
25+
'paths' => ['./'],
26+
'exclude' => ['vendor', 'qa', 'tools'],
27+
'otherArguments' => '--colors',
28+
// 'ignoreErrorsOnExit' => true,
29+
],
30+
'phpcs' => [
31+
'executablePath' => 'tools/php71/phpcs',
32+
'paths' => ['./'],
33+
'standard' => './qa/psr12-ruleset.xml', // or predefined rules: Squiz, PSR12, Generic, PEAR
34+
'ignore' => ['vendor', 'tools'],
35+
'error-severity' => 1,
36+
'warning-severity' => 6,
37+
'otherArguments' => '--report=summary --parallel=2',
38+
],
39+
'phpcbf' => [
40+
'usePhpcsConfiguration' => true,
41+
// 'executablePath' => 'tools/php71/phpcbf',
42+
// 'paths' => ['./'],
43+
// 'standard' => './qa/psr12-ruleset.xml', // or predefined rules: Squiz, PSR12, Generic, PEAR
44+
// 'ignore' => ['vendor'], // Se podría configurar en el standard directamente
45+
// 'error-severity' => 1,
46+
// 'warning-severity' => 6,
47+
],
48+
'phpmd' => [
49+
'executablePath' => 'tools/php71/phpmd',
50+
'paths' => ['./src/'],
51+
'rules' => './qa/phpmd-ruleset.xml', // or predefined rules cleancode,codesize,controversial,design,naming,unusedcode
52+
'exclude' => ['vendor'], // Se podría configurar en las rules directamente
53+
// 'otherArguments' => '--strict',
54+
// 'ignoreErrorsOnExit' => true,
55+
],
56+
'phpcpd' => [
57+
'executablePath' => 'tools/php71/phpcpd',
58+
'paths' => ['./'],
59+
'exclude' => ['vendor', 'tests', 'tools'],
60+
// 'otherArguments' => '--min-lines=5',
61+
],
62+
'security-checker' => [
63+
'executablePath' => 'tools/php71/local-php-security-checker',
64+
// 'otherArguments' => '-format json',
65+
],
66+
];

src/ConfigurationFile/CliArguments.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ class CliArguments
2929
/** @var int */
3030
protected $processes;
3131

32+
/** @var string */
33+
protected $configFile;
34+
3235
public function __construct(
3336
string $tool,
3437
string $execution,
3538
$ignoreErrorsOnExit,
3639
string $otherArguments,
3740
string $executablePath,
3841
string $paths,
39-
int $processes
42+
int $processes,
43+
string $configFile = ''
4044
) {
4145
$this->tool = $tool;
4246
$this->execution = $execution;
@@ -45,6 +49,7 @@ public function __construct(
4549
$this->executablePath = $executablePath;
4650
$this->paths = $paths;
4751
$this->processes = $processes;
52+
$this->configFile = $configFile;
4853
}
4954

5055
public function overrideArguments(array $configurationFile): array
@@ -94,6 +99,11 @@ protected function overrideToolArguments(array $toolConfiguration): array
9499
return $toolConfiguration;
95100
}
96101

102+
public function getConfigFile(): string
103+
{
104+
return $this->configFile;
105+
}
106+
97107
public function getTool(): string
98108
{
99109
return $this->tool;

src/ConfigurationFile/FileReader.php

+43-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Wtyd\GitHooks\ConfigurationFile;
66

7+
use InvalidArgumentException;
78
use Symfony\Component\Yaml\Exception\ParseException;
89
use Symfony\Component\Yaml\Yaml;
910
use Wtyd\GitHooks\ConfigurationFile\Exception\ConfigurationFileNotFoundException;
@@ -20,17 +21,39 @@ public function __construct()
2021
}
2122

2223
/**
23-
* @return array File configuration githooks.yml in associative array format.
24+
* @return array File configuration in associative array format.
2425
*
2526
* @throws \Wtyd\GitHooks\ConfigurationFile\Exception\ParseConfigurationFileException
2627
* @throws \Wtyd\GitHooks\ConfigurationFile\Exception\ConfigurationFileNotFoundException
28+
* @throws \InvalidArgumentException
2729
*/
28-
public function readFile(): array
30+
public function readFile(string $configFile = ''): array
2931
{
30-
$configurationFilePath = $this->findConfigurationFile();
32+
$configurationFilePath = '';
33+
if (!empty($configFile)) {
34+
// dd(strpos($configFile, DIRECTORY_SEPARATOR) === 0, $configFile);
35+
if (strpos($configFile, DIRECTORY_SEPARATOR) === 0) {
36+
$configurationFilePath = $configFile;
37+
} else {
38+
$configurationFilePath = $this->rootPath . DIRECTORY_SEPARATOR . $configFile;
39+
}
40+
} else {
41+
$configurationFilePath = $this->findConfigurationFile();
42+
}
3143

3244
try {
33-
$configurationFile = Yaml::parseFile($configurationFilePath);
45+
$fileExtension = pathinfo($configurationFilePath, PATHINFO_EXTENSION);
46+
// dd($fileExtension);
47+
if ($fileExtension === 'yml' || $fileExtension === 'yaml') {
48+
$configurationFile = Yaml::parseFile($configurationFilePath);
49+
} elseif ($fileExtension === 'php') {
50+
$configurationFile = require $configurationFilePath;
51+
if (!is_array($configurationFile)) {
52+
throw new ParseConfigurationFileException('PHP configuration file does not return an array.');
53+
}
54+
} else {
55+
throw new InvalidArgumentException('Unsupported file type.');
56+
}
3457
} catch (ParseException $exception) {
3558
throw ParseConfigurationFileException::forMessage($exception->getMessage());
3659
}
@@ -39,19 +62,29 @@ public function readFile(): array
3962
}
4063

4164
/**
42-
* Searchs configuration file 'githooks.yml' on root path and qa/ directory
65+
* Searchs configuration file on root path and qa/ directory
4366
*
4467
* @return string The path of configuration file
4568
*
4669
* @throws \Wtyd\GitHooks\ConfigurationFile\Exception\ConfigurationFileNotFoundException
4770
*/
4871
public function findConfigurationFile(): string
4972
{
50-
if (file_exists("$this->rootPath/githooks.yml")) {
51-
$configFile = "$this->rootPath/githooks.yml";
52-
} elseif (file_exists("$this->rootPath/qa/githooks.yml")) {
53-
$configFile = "$this->rootPath/qa/githooks.yml";
54-
} else {
73+
$possiblePaths = [
74+
"$this->rootPath/githooks.php",
75+
"$this->rootPath/qa/githooks.php",
76+
"$this->rootPath/githooks.yml",
77+
"$this->rootPath/qa/githooks.yml"
78+
];
79+
80+
foreach ($possiblePaths as $path) {
81+
if (file_exists($path)) {
82+
$configFile = $path;
83+
break;
84+
}
85+
}
86+
87+
if (!isset($configFile)) {
5588
throw new ConfigurationFileNotFoundException();
5689
}
5790

src/ConfigurationFile/FileReaderFake.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ public function __construct(string $rootPath = null)
2121
*
2222
* @return array
2323
*/
24-
public function readFile(): array
24+
public function readFile(string $configFile = ''): array
2525
{
2626
if (empty($this->mockConfigurationFile)) {
27-
return parent::readFile();
27+
return parent::readFile($configFile);
2828
} else {
2929
return $this->mockConfigurationFile;
3030
}

src/ConfigurationFile/ReadConfigurationFileAction.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct(FileReader $fileReader)
1616

1717
public function __invoke(CliArguments $cliArguments): ConfigurationFile
1818
{
19-
$file = $this->fileReader->readfile();
19+
$file = $this->fileReader->readfile($cliArguments->getConfigFile());
2020

2121
$file = $cliArguments->overrideArguments($file);
2222

0 commit comments

Comments
 (0)