-
-
Notifications
You must be signed in to change notification settings - Fork 47
Check: adds an option to specifiy an autoloader file to be included #489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,10 @@ | |
|
|
||
| use Arkitect\CLI\Baseline; | ||
| use Arkitect\CLI\ConfigBuilder; | ||
| use Arkitect\CLI\Printer\Printer; | ||
| use Arkitect\CLI\Printer\PrinterFactory; | ||
| use Arkitect\CLI\Progress\DebugProgress; | ||
| use Arkitect\CLI\Progress\Progress; | ||
| use Arkitect\CLI\Progress\ProgressBarProgress; | ||
| use Arkitect\CLI\Runner; | ||
| use Arkitect\CLI\TargetPhpVersion; | ||
|
|
@@ -16,6 +18,7 @@ | |
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\ConsoleOutputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Webmozart\Assert\Assert; | ||
|
|
||
| class Check extends Command | ||
| { | ||
|
|
@@ -26,6 +29,7 @@ class Check extends Command | |
| private const SKIP_BASELINE_PARAM = 'skip-baseline'; | ||
| private const IGNORE_BASELINE_LINENUMBERS_PARAM = 'ignore-baseline-linenumbers'; | ||
| private const FORMAT_PARAM = 'format'; | ||
| private const AUTOLOAD_PARAM = 'autoload'; | ||
|
|
||
| private const GENERATE_BASELINE_PARAM = 'generate-baseline'; | ||
| private const DEFAULT_RULES_FILENAME = 'phparkitect.php'; | ||
|
|
@@ -95,6 +99,12 @@ protected function configure(): void | |
| InputOption::VALUE_OPTIONAL, | ||
| 'Output format: text (default), json, gitlab', | ||
| 'text' | ||
| ) | ||
| ->addOption( | ||
| self::AUTOLOAD_PARAM, | ||
| 'a', | ||
| InputOption::VALUE_REQUIRED, | ||
| 'Specify an autoload file to use', | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -123,20 +133,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
| $this->printHeadingLine($output); | ||
|
|
||
| $config = ConfigBuilder::loadFromFile($rulesFilename) | ||
| ->autoloadFilePath($input->getOption(self::AUTOLOAD_PARAM)) | ||
| ->stopOnFailure($stopOnFailure) | ||
| ->targetPhpVersion(TargetPhpVersion::create($phpVersion)) | ||
| ->baselineFilePath(Baseline::resolveFilePath($useBaseline, self::DEFAULT_BASELINE_FILENAME)) | ||
| ->ignoreBaselineLinenumbers($ignoreBaselineLinenumbers) | ||
| ->skipBaseline($skipBaseline) | ||
| ->format($format); | ||
|
|
||
| $printer = PrinterFactory::create($config->getFormat()); | ||
|
|
||
| $progress = $verbose ? new DebugProgress($output) : new ProgressBarProgress($output); | ||
| $this->requireAutoload($config->getAutoloadFilePath(), $output); | ||
| $printer = $this->createPrinter($config->getFormat(), $output); | ||
| $progress = $this->createProgress($verbose, $output); | ||
| $baseline = $this->createBaseline($config->isSkipBaseline(), $config->getBaselineFilePath(), $output); | ||
|
|
||
| $baseline = Baseline::create($config->isSkipBaseline(), $config->getBaselineFilePath()); | ||
|
|
||
| $baseline->getFilename() && $output->writeln("Baseline file '{$baseline->getFilename()}' found"); | ||
| $output->writeln("Config file '$rulesFilename' found\n"); | ||
|
|
||
| $runner = new Runner(); | ||
|
|
@@ -177,6 +186,45 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * @psalm-suppress UnresolvableInclude | ||
| */ | ||
| protected function requireAutoload(?string $filePath, OutputInterface $output): void | ||
|
||
| { | ||
| if (null === $filePath) { | ||
| return; | ||
| } | ||
|
|
||
| Assert::file($filePath, "Cannot find '$filePath'"); | ||
|
|
||
| require_once $filePath; | ||
|
|
||
| $output->writeln("Autoload file '$filePath' added"); | ||
| } | ||
|
|
||
| protected function createPrinter(string $format, OutputInterface $output): Printer | ||
| { | ||
| $output->writeln("Output format: $format"); | ||
|
|
||
| return PrinterFactory::create($format); | ||
| } | ||
|
|
||
| protected function createProgress(bool $verbose, OutputInterface $output): Progress | ||
| { | ||
| $output->writeln('Progress: '.($verbose ? 'debug' : 'bar')); | ||
|
|
||
| return $verbose ? new DebugProgress($output) : new ProgressBarProgress($output); | ||
| } | ||
|
|
||
| protected function createBaseline(bool $skipBaseline, ?string $baselineFilePath, OutputInterface $output): Baseline | ||
|
||
| { | ||
| $baseline = Baseline::create($skipBaseline, $baselineFilePath); | ||
|
|
||
| $baseline->getFilename() && $output->writeln("Baseline file '{$baseline->getFilename()}' found"); | ||
|
|
||
| return $baseline; | ||
| } | ||
|
|
||
| protected function printHeadingLine(OutputInterface $output): void | ||
| { | ||
| $app = $this->getApplication(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| spl_autoload_register(function ($class): void { | ||
| $classmap = [ | ||
| 'Autoload\Services\UserService' => __DIR__.'/src/Service/UserService.php', | ||
| 'Autoload\Model\User' => __DIR__.'/src/Model/User.php', | ||
| 'Autoload\Model\UserInterface' => __DIR__.'/src/Model/UserInterface.php', | ||
| ]; | ||
|
|
||
| $path = $classmap[$class] ?? null; | ||
|
|
||
| if (null === $path) { | ||
| return; | ||
| } | ||
|
|
||
| if (!file_exists($path)) { | ||
| return; | ||
| } | ||
|
|
||
| require_once $path; | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| use Arkitect\Analyzer\ClassDescription; | ||
| use Arkitect\ClassSet; | ||
| use Arkitect\CLI\Config; | ||
| use Arkitect\Expression\Description; | ||
| use Arkitect\Expression\Expression; | ||
| use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces; | ||
| use Arkitect\Rules\Rule; | ||
| use Arkitect\Rules\Violation; | ||
| use Arkitect\Rules\ViolationMessage; | ||
| use Arkitect\Rules\Violations; | ||
|
|
||
| return static function (Config $config): void { | ||
| // a dummy rule to check if the class is autoloaded | ||
| // is_a with 'true' passed as the third parameter triggers the autoloader | ||
| $autoload_rule = new class('Autoload\Model\UserInterface') implements Expression { | ||
| public string $implements; | ||
|
|
||
| public function __construct(string $implements) | ||
| { | ||
| $this->implements = $implements; | ||
| } | ||
|
|
||
| public function describe(ClassDescription $theClass, string $because): Description | ||
| { | ||
| return new Description("{$theClass->getFQCN()} should implement {$this->implements}", $because); | ||
| } | ||
|
|
||
| public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void | ||
| { | ||
| if (is_a($theClass->getFQCN(), $this->implements, true)) { | ||
| return; | ||
| } | ||
|
|
||
| $violation = Violation::create( | ||
| $theClass->getFQCN(), | ||
| ViolationMessage::selfExplanatory($this->describe($theClass, $because)), | ||
| $theClass->getFilePath() | ||
| ); | ||
|
|
||
| $violations->add($violation); | ||
| } | ||
| }; | ||
|
|
||
| $class_set = ClassSet::fromDir(__DIR__.'/src'); | ||
|
|
||
| $rule = Rule::allClasses() | ||
| ->except('Autoload\Model\UserInterface') | ||
| ->that(new ResideInOneOfTheseNamespaces('Autoload\Model')) | ||
| ->should($autoload_rule) | ||
| ->because('we want check if the class is autoloaded'); | ||
|
|
||
| $config | ||
| ->add($class_set, $rule); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Autoload\Model; | ||
|
|
||
| class User implements UserInterface | ||
| { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Autoload\Model; | ||
|
|
||
| interface UserInterface | ||
| { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Autoload\Services; | ||
|
|
||
| class UserService | ||
| { | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.