From 90c4e470abb2cdf9ab2fda4613395f66476a2f28 Mon Sep 17 00:00:00 2001 From: Alessandro Minoccheri Date: Thu, 8 Oct 2020 22:41:44 +0200 Subject: [PATCH] upgrade to level 3 of psalm --- CHANGELOG.md | 8 ++- psalm.xml | 2 +- src/Badge.php | 5 ++ src/Render/LocalSvgRenderer.php | 8 +++ src/UI/Command.php | 21 ++++--- src/UI/SingleCommandApplication.php | 11 +++- src/ValueObject/InputRequest.php | 86 +++++++++++++++++++++++++++++ 7 files changed, 129 insertions(+), 12 deletions(-) create mode 100644 src/ValueObject/InputRequest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 01703d1..0c89c65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [v2.0.4] - 2020-10-09 + +### Changed +* upgrade psalm error level to 3 + ## [v2.0.3] - 2020-10-08 ### Changed @@ -110,7 +115,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - stable release for poser -[Unreleased]: https://github.com/badges/poser/compare/v2.0.3...HEAD +[Unreleased]: https://github.com/badges/poser/compare/v2.0.4...HEAD +[v2.0.4]: https://github.com/badges/poser/tree/v2.0.3 [v2.0.3]: https://github.com/badges/poser/tree/v2.0.3 [v2.0.2]: https://github.com/badges/poser/tree/v2.0.2 [v2.0.1]: https://github.com/badges/poser/tree/v2.0.1 diff --git a/psalm.xml b/psalm.xml index d044bbb..740adf8 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,6 @@ templatesDirectory) { + throw new \InvalidArgumentException('TemplateDirectory cannot be null'); + } + $filepath = \sprintf('%s/%s.svg', $this->templatesDirectory, $style); if (!\file_exists($filepath)) { @@ -70,6 +74,10 @@ private function getTemplate(string $style): string private function stringWidth(string $text): float { + if (null === $this->textSizeCalculator) { + throw new \InvalidArgumentException('TextSizeCalculator cannot be null'); + } + return $this->textSizeCalculator->calculateWidth($text); } diff --git a/src/UI/Command.php b/src/UI/Command.php index 75ad5bc..976a51b 100644 --- a/src/UI/Command.php +++ b/src/UI/Command.php @@ -7,6 +7,7 @@ use PUGX\Poser\Render\SvgFlatRender; use PUGX\Poser\Render\SvgFlatSquareRender; use PUGX\Poser\Render\SvgPlasticRender; +use PUGX\Poser\ValueObject\InputRequest; use Symfony\Component\Console\Command\Command as BaseCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -83,20 +84,24 @@ protected function configure(): void /** * @throws \Exception + * @throws \InvalidArgumentException */ protected function execute(InputInterface $input, OutputInterface $output): int { - $subject = $input->getArgument('subject'); - $status = $input->getArgument('status'); - $color = $input->getArgument('color'); - $style = (string) $input->getOption('style'); - $format = (string) $input->getOption('format'); + $inputRequest = InputRequest::createFromInput($input); try { - $imageContent = $this->poser->generate($subject, $status, $color, $style, $format); + $imageContent = $this->poser->generate( + $inputRequest->getSubject(), + $inputRequest->getStatus(), + $inputRequest->getColor(), + $inputRequest->getStyle(), + $inputRequest->getFormat() + ); - if ($input->getOption('path')) { - $this->storeImage($output, $input->getOption('path'), (string) $imageContent); + $path = $input->getOption('path'); + if ($path && \is_string($path)) { + $this->storeImage($output, $path, (string) $imageContent); } else { $this->flushImage($output, (string) $imageContent); } diff --git a/src/UI/SingleCommandApplication.php b/src/UI/SingleCommandApplication.php index 2fca0aa..877e738 100644 --- a/src/UI/SingleCommandApplication.php +++ b/src/UI/SingleCommandApplication.php @@ -37,14 +37,21 @@ class SingleCommandApplication extends Application * * @param Command $command The single (accessible) command for this application * @param string $version The version of the application + * + * @throws \InvalidArgumentException */ public function __construct(Command $command, string $version = 'UNKNOWN') { - parent::__construct($command->getName(), $version); + $commandName = $command->getName(); + if (null === $commandName) { + throw new \InvalidArgumentException('command cannot be null'); + } + + parent::__construct($commandName, $version); // Add the given command as single (accessible) command. $this->add($command); - $this->commandName = (string) $command->getName(); + $this->commandName = (string) $commandName; // Override the Application's definition so that it does not // require a command name as first argument. diff --git a/src/ValueObject/InputRequest.php b/src/ValueObject/InputRequest.php new file mode 100644 index 0000000..49c3487 --- /dev/null +++ b/src/ValueObject/InputRequest.php @@ -0,0 +1,86 @@ +subject = $subject; + $this->status = $status; + $this->color = $color; + $this->style = $style; + $this->format = $format; + } + + public static function createFromInput(InputInterface $input): self + { + $subject = $input->getArgument('subject'); + $status = $input->getArgument('status'); + $color = $input->getArgument('color'); + $style = $input->getOption('style'); + $format = $input->getOption('format'); + + if (!\is_string($subject)) { + throw new \InvalidArgumentException('subject is not a string'); + } + + if (!\is_string($status)) { + throw new \InvalidArgumentException('status is not a string'); + } + + if (!\is_string($color)) { + throw new \InvalidArgumentException('color is not a string'); + } + + if (!\is_string($style)) { + throw new \InvalidArgumentException('style is not a string'); + } + + if (!\is_string($format)) { + $format = BADGE::DEFAULT_FORMAT; + } + + return new self($subject, $status, $color, $style, $format); + } + + public function getSubject(): string + { + return $this->subject; + } + + public function getStatus(): string + { + return $this->status; + } + + public function getColor(): string + { + return $this->color; + } + + public function getStyle(): string + { + return $this->style; + } + + public function getFormat(): string + { + return $this->format; + } +}