-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from evgenij-vy/confi-dir-v2
Add configuration reader and configuration dir argument
- Loading branch information
Showing
8 changed files
with
179 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the box project. | ||
* | ||
* (c) Anatoliy Melnikov <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Aeliot\PhpCsFixerBaseline\Service; | ||
|
||
use PhpCsFixer\Config; | ||
use PhpCsFixer\ConfigInterface; | ||
use PhpCsFixer\Finder; | ||
|
||
final class ConsoleOptionsReader | ||
{ | ||
private string $rootDirectory; | ||
|
||
/** @var array<string, string> */ | ||
private array $option; | ||
|
||
public function __construct() | ||
{ | ||
$this->option = getopt( | ||
'ab:c:d:f:w:', | ||
['absolute', 'baseline:', 'config:', 'config-dir:', 'finder:', 'workdir:'] | ||
); | ||
$this->rootDirectory = $this->getOptionValue('d','config-dir', ''); | ||
} | ||
|
||
/** | ||
* @return array{ | ||
* baselinePath: string, | ||
* config: Config|ConfigInterface, | ||
* finder: Finder, | ||
* relative?: bool, | ||
* workdir?: string|null | ||
* } | ||
*/ | ||
public function getAsArray(): array | ||
{ | ||
return [ | ||
'baselinePath' => $this->getBaselinePath(), | ||
'config' => $this->getConfig(), | ||
'finder' => $this->getFinder(), | ||
'relative' => $this->getRelative(), | ||
'workdir' => $this->getWorkdir(), | ||
]; | ||
} | ||
|
||
private function getBaselinePath(): string | ||
{ | ||
return $this->getAbsolutePath($this->getOptionValue('b', 'baseline', '.php-cs-fixer-baseline.json')); | ||
} | ||
|
||
private function getConfig(): Config | ||
{ | ||
$configPath = $this->getAbsolutePath($this->getOptionValue('c', 'config', '.php-cs-fixer.dist.php')); | ||
|
||
return require $configPath; | ||
} | ||
|
||
private function getFinder(): Finder | ||
{ | ||
$finderPath = $this->getAbsolutePath($this->getOptionValue('f', 'finder', '.php-cs-fixer-finder.php')); | ||
|
||
return require $finderPath; | ||
} | ||
|
||
private function getWorkdir(): ?string | ||
{ | ||
return $this->getOptionValue('w', 'workdir', null); | ||
} | ||
|
||
private function getRelative(): ?bool | ||
Check failure on line 80 in src/Service/ConsoleOptionsReader.php GitHub Actions / Static analyze with PHPStan
|
||
{ | ||
return !$this->getOptionValue('a', 'relative', false); | ||
} | ||
|
||
private function getAbsolutePath(string $path): string | ||
{ | ||
$path = $this->rootDirectory . $path; | ||
|
||
if (preg_match('#^(?:[[:alpha:]]:[/\\\\]|/)#', $path)) { | ||
return $path; | ||
} | ||
|
||
return getcwd() . '/' . $path; | ||
} | ||
|
||
private function getOptionValue(string $short, string $long, bool|null|string $default): bool|null|string | ||
{ | ||
if (\array_key_exists($short, $this->option) && \array_key_exists($long, $this->option)) { | ||
throw new \InvalidArgumentException(sprintf('%s is duplicated', $long)); | ||
} | ||
|
||
return $this->option[$short] ?? $this->option[$long] ?? $default; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Aeliot\PhpCsFixerBaseline\Test\Unit\Functional; | ||
|
||
use PHPUnit\Framework\Attributes\CoversNothing; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversNothing] | ||
final class CommandLineTest extends TestCase | ||
{ | ||
public function testCommandLine(): void | ||
{ | ||
$baselinePath = __DIR__ . '/../../config/.php-cs-fixer-baseline.json'; | ||
|
||
if (file_exists($baselinePath)) { | ||
unlink($baselinePath); | ||
} | ||
|
||
shell_exec('php bin/pcsf-baseline -d tests/config/'); | ||
|
||
self::assertFileExists($baselinePath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"config_hash": 309824811, | ||
"relative": true, | ||
"hashes": { | ||
"tests\/fixtures\/file-for-calculation-of-hash.php": { | ||
"hash": 4266623405 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
return $finder = (new PhpCsFixer\Finder()) | ||
->files() | ||
->name('file-for-calculation-of-hash.php') | ||
->in(__DIR__ . '/../fixtures/'); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
use Aeliot\PhpCsFixerBaseline\Service\FilterFactory; | ||
|
||
$rules = [ | ||
'header_comment' => [ | ||
'header' => <<<'EOF' | ||
This file is part of the box project. | ||
(c) Anatoliy Melnikov <[email protected]> | ||
This source file is subject to the MIT license that is bundled | ||
with this source code in the file LICENSE. | ||
EOF, | ||
], | ||
'phpdoc_align' => ['align' => 'left'], | ||
]; | ||
|
||
$config = (new PhpCsFixer\Config()) | ||
->setRiskyAllowed(true) | ||
->setRules($rules); | ||
|
||
/** @var PhpCsFixer\Finder $finder */ | ||
$finder = require __DIR__ . '/.php-cs-fixer-finder.php'; | ||
$finder->filter((new FilterFactory())->createFilter(__DIR__ . '/.php-cs-fixer-baseline.json', $config)); | ||
|
||
return $config->setFinder($finder); |