Skip to content

Commit

Permalink
Fixed some code style warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiang committed Feb 14, 2023
1 parent 8ea1792 commit 54845f8
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 73 deletions.
73 changes: 37 additions & 36 deletions src/Cli/Command/ExceptionGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Fabiang\ExceptionGenerator\Cli\Command;

use Fabiang\ExceptionGenerator\Cli\Console\Application;
use Fabiang\ExceptionGenerator\Generator\CreateException;
use Fabiang\ExceptionGenerator\Generator\RecursiveNamespaceResolver;
use Fabiang\ExceptionGenerator\Generator\RecursiveParentExceptionResolver;
Expand All @@ -23,6 +24,7 @@
use function array_reverse;
use function getcwd;
use function is_array;
use function is_string;
use function realpath;
use function substr;

Expand Down Expand Up @@ -71,17 +73,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$path = getcwd();
}

if (! is_string($path)) {
$path = '';
}

/** @var QuestionHelper $questionHelper */
$questionHelper = $this->getHelper('question');

$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber(new CreateExceptionListener($output, $input, $questionHelper));
$namespaceResolver = new RecursiveNamespaceResolver($eventDispatcher);

$namespace = $namespaceResolver->resolveNamespace($path);
$templatePathMatcher = new TemplatePathMatcher($path, $this->getApplication()->getHome());
$namespace = $namespaceResolver->resolveNamespace($path);

/** @var Application $application */
$application = $this->getApplication();

$templatePath = $this->realpath($input->getOption('template-path')) ?: null;
$templatePathMatcher = new TemplatePathMatcher($path, $application->getHome() ?? '');

$templatePath = $this->realpath($input->getOption('template-path')) ?: '';
$templateResolver = new TemplateResolver($templatePath, $templatePathMatcher);

$exceptionTemplate = $templateResolver->resolve('exception.phtml');
Expand Down Expand Up @@ -109,49 +119,40 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$prevParentNamespace = $parentExceptionNamespace;
$parentExceptionNamespace = $namespaceResolver->resolveNamespace($parentExceptionDir);

$output->writeln(
'BaseExceptionPath: "' . $parentExceptionDir . '"',
OutputInterface::VERBOSITY_VERY_VERBOSE
);
$output->writeln(
'BaseExceptionNamespace: "' . $parentExceptionNamespace . '"',
OutputInterface::VERBOSITY_VERY_VERBOSE
);

$parentExceptionCreator = new CreateException(
$eventDispatcher,
$templateRenderer,
false,
$output,
$input
);

$parentExceptionCreator->create(
$parentExceptionNamespace,
$parentExceptionDir,
$prevParentNamespace
);
if ($parentExceptionNamespace !== null) {
$output->writeln(
'BaseExceptionPath: "' . $parentExceptionDir . '"',
OutputInterface::VERBOSITY_VERY_VERBOSE
);
$output->writeln(
'BaseExceptionNamespace: "' . $parentExceptionNamespace . '"',
OutputInterface::VERBOSITY_VERY_VERBOSE
);

$parentExceptionCreator = new CreateException(
$eventDispatcher,
$templateRenderer,
false
);

$parentExceptionCreator->create(
$parentExceptionNamespace,
$parentExceptionDir,
$prevParentNamespace
);
}
}
}
}

if (
$parentExceptionNamespace && false === $useParents ||
($parentExceptionNamespace && false !== $useParents)
) {
$output->writeln('BaseExceptionPath: not found/used', OutputInterface::VERBOSITY_VERY_VERBOSE);
}

$namespaceQuestion = new Question("Is this the correct namespace: [$namespace]?", $namespace);
$inputNamespace = $questionHelper->ask($input, $output, $namespaceQuestion);
$output->writeln('Namespace set to "' . $inputNamespace . '"');

$exceptionCreator = new CreateException(
$eventDispatcher,
$templateRenderer,
$input->getOption('overwrite'),
$output,
$input
$input->getOption('overwrite')
);

$exceptionCreator->create($inputNamespace, $path . '/Exception', $parentExceptionNamespace);
Expand All @@ -162,7 +163,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/**
* Realpath.
*/
private function realpath(?string $path): string|bool
private function realpath(?string $path): string|false
{
if (null === $path) {
return '';
Expand Down
4 changes: 2 additions & 2 deletions src/Cli/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public function getDefinition(): InputDefinition
/**
* Get path to home directory.
*/
public function getHome(): string
public function getHome(): ?string
{
if (null === $this->home) {
$this->home = getenv('HOME');
$this->home = getenv('HOME') ?: null;
}

return $this->home;
Expand Down
7 changes: 4 additions & 3 deletions src/Generator/CreateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public function __construct(
) {
$this->templateRenderer = $templateRenderer;
$this->eventDispatcher = $eventDispatcher;
$this->overwrite = (bool) $overwrite;
$this->overwrite = $overwrite;
}

/**
* creates the exception classes and the exception folder
*/
public function create(string $namespace, string $path, ?string $usePath = null)
public function create(string $namespace, string $path, ?string $usePath = null): void
{
$exceptionNames = ExceptionClassNames::getExceptionClassNames();

Expand Down Expand Up @@ -95,8 +95,9 @@ protected function validate(string $fileName): bool
if ($this->overwrite || ! $fileExists) {
return true;
}

// if user has chosen to skip overwriting all existing files, then return early
if ($this->skipAll && $fileExists) {
if ($this->skipAll) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Generator/RecursiveNamespaceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ public function resolveNamespace(string $path): ?string
foreach ($directory as $item) {
$breakEvent = new FileEvent($item);
$eventDispatcher->dispatch($breakEvent, 'file.break');
if (false !== $breakEvent->isPropagationStopped()) {
if (true === $breakEvent->isPropagationStopped()) {
break 2;
}
}
$loopedPaths[] = basename($path);
$path = dirname($path) !== 'vfs:' ? dirname($path) : 'vfs://';
//break early cuz DirectoryIterator can't handle vfs root folder
} while ((0 === count($directory) || ! $breakEvent->isPropagationStopped()) && $path !== 'vfs://');
} while (0 === count($directory) || $path !== 'vfs://');

return $namespace;
}
Expand Down
11 changes: 3 additions & 8 deletions src/Generator/RecursiveParentExceptionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ class RecursiveParentExceptionResolver
private const VFSSTREAM_PREFIX = 'vfs:';
private const VFSSTREAM_URL = self::VFSSTREAM_PREFIX . '//';

/**
* provides a namespace dpending on looped folders after searching for parent exceptions, which you should use
*/
protected string $providedNamespace;

public function __construct(protected EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
Expand All @@ -48,7 +43,7 @@ public function resolveExceptionDirs(string $path): ?array
{
$exceptionDirArray = null;
$eventDispatcher = $this->eventDispatcher;
$loopedPaths[] = basename($path);
$loopedPaths = [basename($path)];
$path = dirname($path);

// loop as long a break listener doesn't stop propagation or we have empty directories
Expand All @@ -71,15 +66,15 @@ public function resolveExceptionDirs(string $path): ?array
foreach ($directory as $item) {
$breakEvent = new FileEvent($item);
$eventDispatcher->dispatch($breakEvent, 'file.break');
if (false !== $breakEvent->isPropagationStopped()) {
if (true === $breakEvent->isPropagationStopped()) {
break 2;
}
}

$path = dirname($path) !== static::VFSSTREAM_PREFIX ? dirname($path) : static::VFSSTREAM_URL;
$loopedPaths[] = basename($path);
//break early cuz DirectoryIterator can't handle vfs root folder
} while ((0 === count($directory) || ! $breakEvent->isPropagationStopped()) && $path !== static::VFSSTREAM_URL);
} while (0 === count($directory) || $path !== static::VFSSTREAM_URL);

return $exceptionDirArray;
}
Expand Down
10 changes: 6 additions & 4 deletions src/Generator/TemplateRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

class TemplateRenderer
{
public function __construct(protected ?PhpRenderer $renderer = null)
protected PhpRenderer $renderer;

public function __construct(?PhpRenderer $renderer = null)
{
$this->renderer = $renderer;
if (null === $renderer) {
$this->renderer = new PhpRenderer();
$renderer = new PhpRenderer();
}

$this->renderer = $renderer;
$this->renderer->setResolver(new TemplateMapResolver());
}

Expand Down Expand Up @@ -67,7 +69,7 @@ public function render(string $namespace, ?string $use = null, ?string $exceptio
return $this->renderer->render($model);
}

private function getUsername(): string
private function getUsername(): ?string
{
if (defined('USER')) {
return USER;
Expand Down
4 changes: 2 additions & 2 deletions src/Listener/CreateExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public function onSkippedCreation(CreateExceptionEvent $event): void
/**
* Overwriting of all files event.
*/
public function onOverwriteAll(CreateExceptionEvent $event): void
public function onOverwriteAll(): void
{
$this->output->writeln('Overwriting all existing files!');
}

/**
* Skip confirmation to overwrite existing files event.
*/
public function onSkipOverwriteAll(CreateExceptionEvent $event): void
public function onSkipOverwriteAll(): void
{
$this->output->writeln('Skipped overwriting all existing files.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Resolver/ComposerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ComposerResolver implements ResolverInterface
/**
* {@inheritDoc}
*/
public function resolve(string $path, array $loopedDirectories): string|bool
public function resolve(string $path, array $loopedDirectories): string|false
{
$namespace = false;
$jsonFile = file_get_contents($path);
Expand Down
3 changes: 2 additions & 1 deletion src/Resolver/NamespaceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ class NamespaceResolver implements ResolverInterface
private const T_NAME_FULLY_QUALIFIED = T_NAME_FULLY_QUALIFIED;
private const T_NAME_QUALIFIED = T_NAME_QUALIFIED;

public function resolve(string $path, array $loopedDirectories): string|bool
public function resolve(string $path, array $loopedDirectories): string|false
{
if (! is_readable($path)) {
throw new RuntimeException('PHP file "' . $path . '" isn\'t readable');
}

$namespace = false;
$tokens = token_get_all(file_get_contents($path));
$type = null;

foreach ($tokens as $token) {
if (is_array($token)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Resolver/ResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface ResolverInterface
/**
* Resolve namespace from file.
*/
public function resolve(string $path, array $loopedDirectories): string|bool;
public function resolve(string $path, array $loopedDirectories): string|false;
}
9 changes: 5 additions & 4 deletions src/TemplateResolver/TemplatePathMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(string $currentDir, string $configPath)
*
* @throws RuntimeException
*/
public function match(string $templateName): bool|string
public function match(string $templateName): false|string
{
if (! is_readable($this->configPath)) {
return false;
Expand All @@ -57,7 +57,7 @@ public function match(string $templateName): bool|string
/**
* trys to get the most matching path or global from config
*/
protected function getPaths(array $configData, string $templateName): bool|string
protected function getPaths(array $configData, string $templateName): false|string
{
if (! isset($configData['templatepath']) || ! is_array($configData['templatepath'])) {
return false;
Expand All @@ -68,7 +68,8 @@ protected function getPaths(array $configData, string $templateName): bool|strin
if (isset($templatePath['projects']) && is_array($templatePath['projects'])) {
$filteredProjects = $this->filterMatchingPaths($templatePath['projects']);

if (false !== ($matchingPath = $this->getMostRelatedPath($filteredProjects, $templateName))) {
$matchingPath = $this->getMostRelatedPath($filteredProjects, $templateName);
if (false !== $matchingPath) {
return $matchingPath;
}
}
Expand Down Expand Up @@ -102,7 +103,7 @@ public function filterMatchingPaths(array $projects): array
/**
* trys to get the most related path where template was found
*/
protected function getMostRelatedPath(array $filteredProjects, string $templateName): bool|string
protected function getMostRelatedPath(array $filteredProjects, string $templateName): false|string
{
uksort($filteredProjects, function ($a, $b) {
$strlenA = strlen($a);
Expand Down
6 changes: 3 additions & 3 deletions src/TemplateResolver/TemplateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TemplateResolver
/**
* Path to template.
*/
protected ?string $templatePath;
protected string $templatePath;

/**
* TemplatePathMatcher instance.
Expand All @@ -23,9 +23,9 @@ class TemplateResolver
/**
* transforms received path to a valid realpath
*/
public function __construct(?string $templatePath, TemplatePathMatcher $templatePathMatcher)
public function __construct(string $templatePath, TemplatePathMatcher $templatePathMatcher)
{
$this->templatePath = rtrim($templatePath ?? '', '/');
$this->templatePath = rtrim($templatePath, '/');
$this->templatePathMatcher = $templatePathMatcher;
}

Expand Down
6 changes: 0 additions & 6 deletions tests/src/Cli/Command/ExceptionGeneratorCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,6 @@ public function executeParents(): void
)
->shouldBeCalled();

$output->writeln(
'BaseExceptionPath: not found/used',
OutputInterface::VERBOSITY_VERY_VERBOSE
)
->shouldBeCalled();

$output->writeln('Writing "vfs://cwd/src/Parent/Exception/BadMethodCallException.php"...')->shouldBeCalled();
$output->writeln('Writing "vfs://cwd/src/Parent/Exception/DomainException.php"...')->shouldBeCalled();
$output->writeln('Writing "vfs://cwd/src/Parent/Exception/InvalidArgumentException.php"...')->shouldBeCalled();
Expand Down

0 comments on commit 54845f8

Please sign in to comment.