From ca08b5caf2ec1b5d96f7613fc156bd32b6ef7ca6 Mon Sep 17 00:00:00 2001 From: Sebastiaan Stok Date: Tue, 15 Aug 2017 09:55:03 +0200 Subject: [PATCH] Correct Style (and apply fixes from PhpStan) --- .php_cs | 2 ++ .travis.yml | 12 +++++-- phpstan.neon | 32 +++++++++++++++++++ src/ConditionErrorMessage.php | 8 ++--- src/Exporter/XmlExporter.php | 2 +- .../Core/ChoiceList/ChoiceLoaderTrait.php | 2 +- .../Factory/PropertyAccessDecorator.php | 29 +++++++---------- .../BaseDateTimeTransformer.php | 6 ++-- .../DataTransformer/BirthdayTransformer.php | 2 +- .../ChoiceToLabelTransformer.php | 3 +- .../ChoiceToValueTransformer.php | 4 ++- .../DateTimeToLocalizedStringTransformer.php | 14 ++++---- .../DateTimeToRfc3339Transformer.php | 10 +++--- .../DateTimeToStringTransformer.php | 14 ++++---- .../DateTimeToTimestampTransformer.php | 12 +++---- .../IntegerToLocalizedStringTransformer.php | 2 +- .../MoneyToLocalizedStringTransformer.php | 22 +++++++------ .../MoneyToStringTransformer.php | 12 +++---- .../NumberToLocalizedStringTransformer.php | 24 ++++---------- .../NumberToStringTransformer.php | 14 ++++---- src/Field/GenericResolvedFieldType.php | 10 +++--- src/Input/ArrayInput.php | 2 +- src/Input/StringQueryInput.php | 2 +- src/Input/XmlInput.php | 2 +- src/Test/SearchIntegrationTestCase.php | 6 ++-- src/Util/XmlUtil.php | 2 +- .../Factory/CachingFactoryDecoratorTest.php | 4 +-- .../Factory/DefaultChoiceListFactoryTest.php | 2 +- .../Factory/PropertyAccessDecoratorTest.php | 4 +-- .../Core/ChoiceList/LazyChoiceListTest.php | 6 ++-- .../Loader/CallbackChoiceLoaderTest.php | 10 +++--- .../ChoiceToValueTransformerTest.php | 4 +-- tests/{ => Extension}/LazyExtensionTest.php | 2 +- tests/{ => Field}/ResolvedFieldTypeTest.php | 2 +- tests/{ => Field}/SearchFieldTest.php | 4 +-- tests/{ => Field}/TypeRegistryTest.php | 2 +- tests/Input/FieldValuesFactoryTest.php | 5 ++- tests/{ => Value}/ValuesBagTest.php | 2 +- tests/{ => Value}/ValuesGroupTest.php | 2 +- 39 files changed, 164 insertions(+), 135 deletions(-) create mode 100644 phpstan.neon rename tests/{ => Extension}/LazyExtensionTest.php (98%) rename tests/{ => Field}/ResolvedFieldTypeTest.php (99%) rename tests/{ => Field}/SearchFieldTest.php (98%) rename tests/{ => Field}/TypeRegistryTest.php (98%) rename tests/{ => Value}/ValuesBagTest.php (98%) rename tests/{ => Value}/ValuesGroupTest.php (98%) diff --git a/.php_cs b/.php_cs index 4868b5de..bc841cf2 100644 --- a/.php_cs +++ b/.php_cs @@ -39,6 +39,8 @@ return PhpCsFixer\Config::create() 'single_import_per_statement' => false, 'strict_comparison' => false, 'strict_param' => true, + // Breaks with phpstan + 'phpdoc_inline_tag' => false, ]) ->setRiskyAllowed(true) ->setFinder( diff --git a/.travis.yml b/.travis.yml index b66e4957..b1abae93 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,8 @@ matrix: #env: coverage=1 - php: '7.1' env: deps='low' + - php: '7.1' + env: lint=1 cache: directories: @@ -22,7 +24,9 @@ before_install: - if [[ $coverage = 1 ]]; then mkdir -p build/logs build/cov; fi - if [[ $coverage = 1 ]]; then wget https://phar.phpunit.de/phpcov.phar; fi - if [[ $coverage = 1 ]]; then wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar; fi - - export PATH="$PATH:$HOME/.composer/vendor/bin" + - if [[ $lint = 1 ]]; then wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.3.2/php-cs-fixer.phar; fi + - if [[ $lint = 1 ]]; then composer global require phpstan/phpstan; fi + - export PATH="$PATH:$HOME/.composer/vendor/bin:./vendor/bin" install: - if [[ $coverage = 1 ]]; then composer require --dev --no-update 'phpunit/php-code-coverage:^4.0.1'; fi @@ -31,8 +35,12 @@ install: - if [[ $deps = 'low' ]]; then composer update --prefer-dist --no-progress --no-suggest --prefer-stable --prefer-lowest --ansi; fi script: - - if [[ $coverage = 1 ]]; then phpdbg -qrr -dmemory_limit=-1 vendor/bin/phpunit --verbose --coverage-php build/cov/coverage-phpunit.cov; else vendor/bin/phpunit --verbose; fi + - export SYMFONY_DEPRECATIONS_HELPER=strict + - if [[ $coverage = 1 ]]; then phpdbg -qrr -dmemory_limit=-1 vendor/bin/phpunit --verbose --coverage-php build/cov/coverage-phpunit.cov; fi - if [[ $coverage = 1 ]]; then phpdbg -qrr phpcov.phar merge --clover build/logs/clover.xml build/cov; fi + - if [[ ! $lint ]]; then vendor/bin/phpunit --verbose; fi + - if [[ $lint = 1 ]]; then php php-cs-fixer.phar fix --dry-run --diff --no-ansi; fi + - if [[ $lint = 1 ]]; then phpstan analyse -c phpstan.neon -l5 --ansi src tests; fi after_success: - if [[ $coverage = 1 ]]; then travis_retry php coveralls.phar; fi diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 00000000..c1412ecb --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,32 @@ +parameters: + autoload_files: + - vendor/autoload.php + ignoreErrors: + #- '#__construct\(\) does not call parent constructor from .+#' + + # ValueHolder guard there own correctness. A ValuesBag never returns a wrong object (collection). + - '#expects Rollerworks\\Component\\Search\\Value\\[a-zA-Z]+, Rollerworks\\Component\\Search\\Value\\ValueHolder given#' + - '#expects Rollerworks\\Component\\Search\\Value\\[a-zA-Z]+\[\], Rollerworks\\Component\\Search\\Value\\ValueHolder\[\] given#' + - '#Call to an undefined method Rollerworks\\Component\\Search\\Value\\ValueHolder\:\:#' + + # False positive + - '#Call to an undefined method Rollerworks\\Component\\Search\\Field\\FieldConfig\:\:finalizeConfig\(\)#' + - '#Call to an undefined method DateTimeInterface\:\:setTimezone\(\)#' + - '#Call to an undefined method Money\\Exception\:\:getMessage\(\)#' + - '#Call to an undefined method Exception\:\:getErrors\(\)#' + - '#expects Rollerworks\\Component\\Search\\Field\\FieldTypeExtension\[\], mixed\[\]\[\] given#' + - '#\(mixed\[\]\[\]\) does not accept iterable\(iterable\(mixed\[\]\)\[\]\)#' + - '#Undefined variable: \$(c|flatChoices)#' + + # Tests + - '#Call to an undefined method Prophecy\\Prophecy\\ObjectProphecy::[a-zA-Z0-9_]+\(\)#' + #- '#Access to an undefined property Prophecy\\Prophecy\\ObjectProphecy::\$[a-zA-Z0-9_]+#' + - '#Call to an undefined method PHPUnit_Framework_MockObject_MockObject::[a-zA-Z0-9_]+\(\)#' + - '#expects\s+[^\s]+, PHPUnit_Framework_MockObject_MockObject(\[\])? given#' + - '#does not accept PHPUnit_Framework_MockObject_MockObject#' + - '#but returns PHPUnit_Framework_MockObject_MockObject#' + - '#Call to an undefined static method Money\\Money\:\:#' + + ## Transformations are tested to ensure there input type is valided. + - '#\:\:transform\(\) expects [^\s]+, [^\s]+ given#' + - '#\:\:reverseTransform\(\) expects [^\s]+, [^\s]+ given#' diff --git a/src/ConditionErrorMessage.php b/src/ConditionErrorMessage.php index 698b928c..7b549532 100644 --- a/src/ConditionErrorMessage.php +++ b/src/ConditionErrorMessage.php @@ -37,7 +37,7 @@ final class ConditionErrorMessage /** * The template for the error message. * - * @var string + * @var string|null */ public $messageTemplate; @@ -80,7 +80,7 @@ final class ConditionErrorMessage * @param int|null $messagePluralization The value for error message pluralization * @param mixed $cause The cause of the error */ - public function __construct(string $path, string $message, string $messageTemplate = null, array $messageParameters = [], int $messagePluralization = null, $cause = null) + public function __construct(string $path, string $message, ?string $messageTemplate = null, array $messageParameters = [], ?int $messagePluralization = null, $cause = null) { $this->path = $path; $this->message = $message; @@ -94,7 +94,7 @@ public static function withMessageTemplate(string $path, string $messageTemplate { return new static( $path, - strtr((string) $messageTemplate, $messageParameters), + strtr($messageTemplate, $messageParameters), $messageTemplate, $messageParameters, $messagePluralization, @@ -124,6 +124,6 @@ public function setTranslatedParameters(array $translatedParameters) */ public function __toString() { - return (string) $this->message; + return $this->message; } } diff --git a/src/Exporter/XmlExporter.php b/src/Exporter/XmlExporter.php index 1b79d5fd..84177bc1 100644 --- a/src/Exporter/XmlExporter.php +++ b/src/Exporter/XmlExporter.php @@ -31,7 +31,7 @@ final class XmlExporter extends AbstractExporter { /** - * @var \DOMDocument + * @var \DOMDocument|null */ private $document; diff --git a/src/Extension/Core/ChoiceList/ChoiceLoaderTrait.php b/src/Extension/Core/ChoiceList/ChoiceLoaderTrait.php index d871c6a3..98b27041 100644 --- a/src/Extension/Core/ChoiceList/ChoiceLoaderTrait.php +++ b/src/Extension/Core/ChoiceList/ChoiceLoaderTrait.php @@ -22,7 +22,7 @@ trait ChoiceLoaderTrait { /** - * @var ArrayChoiceList + * @var ArrayChoiceList|null */ protected $choiceList; diff --git a/src/Extension/Core/ChoiceList/Factory/PropertyAccessDecorator.php b/src/Extension/Core/ChoiceList/Factory/PropertyAccessDecorator.php index 755015de..63993829 100644 --- a/src/Extension/Core/ChoiceList/Factory/PropertyAccessDecorator.php +++ b/src/Extension/Core/ChoiceList/Factory/PropertyAccessDecorator.php @@ -43,23 +43,16 @@ */ final class PropertyAccessDecorator implements ChoiceListFactory { - /** - * @var ChoiceListFactory - */ private $decoratedFactory; - - /** - * @var PropertyAccessor - */ private $propertyAccessor; /** * Decorates the given factory. * * @param ChoiceListFactory $decoratedFactory The decorated factory - * @param null|PropertyAccessor $propertyAccessor The used property accessor + * @param PropertyAccessor|null $propertyAccessor The used property accessor */ - public function __construct(ChoiceListFactory $decoratedFactory, PropertyAccessor $propertyAccessor = null) + public function __construct(ChoiceListFactory $decoratedFactory, ?PropertyAccessor $propertyAccessor = null) { $this->decoratedFactory = $decoratedFactory; $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); @@ -68,7 +61,7 @@ public function __construct(ChoiceListFactory $decoratedFactory, PropertyAccesso /** * Returns the decorated factory. * - * @return ChoiceListFactory The decorated factory + * @return ChoiceListFactory */ public function getDecoratedFactory(): ChoiceListFactory { @@ -76,13 +69,13 @@ public function getDecoratedFactory(): ChoiceListFactory } /** - * {@inheritdoc} + * @inheritdoc * * @param array|\Traversable $choices The choices - * @param null|callable|string|PropertyPath $value The callable or path for + * @param callable|string|PropertyPath|null $value The callable or path for * generating the choice values * - * @return ChoiceList The choice list + * @return ChoiceList */ public function createListFromChoices($choices, $value = null): ChoiceList { @@ -107,13 +100,13 @@ public function createListFromChoices($choices, $value = null): ChoiceList } /** - * {@inheritdoc} + * @inheritdoc * * @param ChoiceLoader $loader The choice loader - * @param null|callable|string|PropertyPath $value The callable or path for + * @param callable|string|PropertyPath|null $value The callable or path for * generating the choice values * - * @return ChoiceList The choice list + * @return ChoiceList */ public function createListFromLoader(ChoiceLoader $loader, $value = null): ChoiceList { @@ -138,7 +131,7 @@ public function createListFromLoader(ChoiceLoader $loader, $value = null): Choic } /** - * {@inheritdoc} + * @inheritdoc * * @param ChoiceList $list The choice list * @param null|array|callable|string|PropertyPath $preferredChoices The preferred choices @@ -147,7 +140,7 @@ public function createListFromLoader(ChoiceLoader $loader, $value = null): Choic * @param null|callable|string|PropertyPath $groupBy The callable or path generating the group names * @param null|array|callable|string|PropertyPath $attr The callable or path generating the HTML attributes * - * @return ChoiceListView The choice list view + * @return ChoiceListView */ public function createView(ChoiceList $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null): ChoiceListView { diff --git a/src/Extension/Core/DataTransformer/BaseDateTimeTransformer.php b/src/Extension/Core/DataTransformer/BaseDateTimeTransformer.php index 78993771..f78d9b5a 100644 --- a/src/Extension/Core/DataTransformer/BaseDateTimeTransformer.php +++ b/src/Extension/Core/DataTransformer/BaseDateTimeTransformer.php @@ -44,12 +44,12 @@ abstract class BaseDateTimeTransformer implements DataTransformer /** * Constructor. * - * @param string $inputTimezone The name of the input timezone - * @param string $outputTimezone The name of the output timezone + * @param string|null $inputTimezone The name of the input timezone + * @param string|null $outputTimezone The name of the output timezone * * @throws UnexpectedTypeException if a timezone is not a string */ - public function __construct(string $inputTimezone = null, string $outputTimezone = null) + public function __construct(?string $inputTimezone = null, ?string $outputTimezone = null) { $this->inputTimezone = $inputTimezone ?? date_default_timezone_get(); $this->outputTimezone = $outputTimezone ?? date_default_timezone_get(); diff --git a/src/Extension/Core/DataTransformer/BirthdayTransformer.php b/src/Extension/Core/DataTransformer/BirthdayTransformer.php index 3988950a..ca3cf277 100644 --- a/src/Extension/Core/DataTransformer/BirthdayTransformer.php +++ b/src/Extension/Core/DataTransformer/BirthdayTransformer.php @@ -109,7 +109,7 @@ private function validateDate(\DateTimeInterface $value) if (!$currentDate) { $currentDate = new \DateTime('now', new \DateTimeZone('UTC')); - $currentDate->setTime(0, 0, 0); + $currentDate->setTime(0, 0); } if ($value > $currentDate) { diff --git a/src/Extension/Core/DataTransformer/ChoiceToLabelTransformer.php b/src/Extension/Core/DataTransformer/ChoiceToLabelTransformer.php index f97ac3a1..b13b13bb 100644 --- a/src/Extension/Core/DataTransformer/ChoiceToLabelTransformer.php +++ b/src/Extension/Core/DataTransformer/ChoiceToLabelTransformer.php @@ -47,7 +47,8 @@ public function transform($choice) $this->choiceListView->initChoicesByLabel(); } - $value = current($this->choiceList->getValuesForChoices([$choice])); + $value = $this->choiceList->getValuesForChoices([$choice]); + $value = current($value); if (!array_key_exists($value, $this->choiceListView->labelsByValue)) { throw new TransformationFailedException(sprintf('The choice "%s" does not exist or is not unique', $choice)); diff --git a/src/Extension/Core/DataTransformer/ChoiceToValueTransformer.php b/src/Extension/Core/DataTransformer/ChoiceToValueTransformer.php index 3589fae9..8935b48e 100644 --- a/src/Extension/Core/DataTransformer/ChoiceToValueTransformer.php +++ b/src/Extension/Core/DataTransformer/ChoiceToValueTransformer.php @@ -31,7 +31,9 @@ public function __construct(ChoiceList $choiceList) public function transform($choice) { - return (string) current($this->choiceList->getValuesForChoices([$choice])); + $value = $this->choiceList->getValuesForChoices([$choice]); + + return (string) current($value); } public function reverseTransform($value) diff --git a/src/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php b/src/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php index 7cdc4ad4..d3afca87 100644 --- a/src/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php +++ b/src/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php @@ -72,14 +72,14 @@ public function __construct(string $inputTimezone = null, string $outputTimezone /** * Transforms a normalized date into a localized date string/array. * - * @param \DateTimeInterface $dateTime A DateTimeInterface object + * @param \DateTimeInterface|null $dateTime A DateTimeInterface object * * @throws TransformationFailedException if the given value is not a \DateTimeInterface * or if the date could not be transformed * - * @return string + * @return string|null */ - public function transform($dateTime) + public function transform($dateTime): ?string { if (null === $dateTime) { return ''; @@ -101,7 +101,7 @@ public function transform($dateTime) /** * Transforms a localized date string/array into a normalized date. * - * @param string|array $value Localized date string/array + * @param string|null $value Localized date string * * @throws TransformationFailedException if the given value is not a string, * if the date could not be parsed @@ -110,11 +110,11 @@ public function transform($dateTime) */ public function reverseTransform($value) { - if (!is_string($value)) { - throw new TransformationFailedException('Expected a string.'); + if (null !== $value && !is_string($value)) { + throw new TransformationFailedException('Expected a string or null.'); } - if ('' === $value) { + if (null === $value || '' === $value) { return null; } diff --git a/src/Extension/Core/DataTransformer/DateTimeToRfc3339Transformer.php b/src/Extension/Core/DataTransformer/DateTimeToRfc3339Transformer.php index f35c528a..c961b2f8 100644 --- a/src/Extension/Core/DataTransformer/DateTimeToRfc3339Transformer.php +++ b/src/Extension/Core/DataTransformer/DateTimeToRfc3339Transformer.php @@ -23,7 +23,7 @@ final class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer /** * {@inheritdoc} */ - public function transform($dateTime) + public function transform($dateTime): ?string { if (null === $dateTime) { return ''; @@ -47,13 +47,13 @@ public function transform($dateTime) /** * {@inheritdoc} */ - public function reverseTransform($rfc3339) + public function reverseTransform($rfc3339): ?\DateTime { - if (!is_string($rfc3339)) { - throw new TransformationFailedException('Expected a string.'); + if (null !== $rfc3339 && !is_string($rfc3339)) { + throw new TransformationFailedException('Expected a string or null.'); } - if ('' === $rfc3339) { + if (null === $rfc3339 || '' === $rfc3339) { return null; } diff --git a/src/Extension/Core/DataTransformer/DateTimeToStringTransformer.php b/src/Extension/Core/DataTransformer/DateTimeToStringTransformer.php index 6af29177..e120e190 100644 --- a/src/Extension/Core/DataTransformer/DateTimeToStringTransformer.php +++ b/src/Extension/Core/DataTransformer/DateTimeToStringTransformer.php @@ -82,9 +82,9 @@ public function __construct(string $inputTimezone = null, string $outputTimezone * instance or if the output timezone * is not supported * - * @return string A value as produced by PHP's date() function + * @return string|null A value as produced by PHP's date() function */ - public function transform($value) + public function transform($value): ?string { if (null === $value) { return ''; @@ -118,14 +118,14 @@ public function transform($value) * * @return \DateTime|null An instance of \DateTime */ - public function reverseTransform($value) + public function reverseTransform($value): ?\DateTime { - if ('' === $value || null === $value) { - return null; + if (null !== $value && !is_string($value)) { + throw new TransformationFailedException('Expected a string or null.'); } - if (!is_string($value)) { - throw new TransformationFailedException('Expected a string.'); + if (null === $value || '' === $value) { + return null; } try { diff --git a/src/Extension/Core/DataTransformer/DateTimeToTimestampTransformer.php b/src/Extension/Core/DataTransformer/DateTimeToTimestampTransformer.php index 0f28e0c0..b50d0395 100644 --- a/src/Extension/Core/DataTransformer/DateTimeToTimestampTransformer.php +++ b/src/Extension/Core/DataTransformer/DateTimeToTimestampTransformer.php @@ -34,7 +34,7 @@ final class DateTimeToTimestampTransformer extends BaseDateTimeTransformer * * @return int|null A timestamp */ - public function transform($dateTime) + public function transform($dateTime): ?int { if (null === $dateTime) { return null; @@ -57,14 +57,14 @@ public function transform($dateTime) * * @return \DateTime|null */ - public function reverseTransform($value) + public function reverseTransform($value): ?\DateTime { - if (null === $value) { - return null; + if (null !== $value && !is_numeric($value)) { + throw new TransformationFailedException('Expected a numeric or null.'); } - if (!is_numeric($value)) { - throw new TransformationFailedException('Expected a numeric.'); + if (null === $value || '' === $value) { + return null; } try { diff --git a/src/Extension/Core/DataTransformer/IntegerToLocalizedStringTransformer.php b/src/Extension/Core/DataTransformer/IntegerToLocalizedStringTransformer.php index 5ef9e1f0..3375e0cc 100644 --- a/src/Extension/Core/DataTransformer/IntegerToLocalizedStringTransformer.php +++ b/src/Extension/Core/DataTransformer/IntegerToLocalizedStringTransformer.php @@ -27,7 +27,7 @@ class IntegerToLocalizedStringTransformer extends NumberToLocalizedStringTransfo * @param bool $grouping Whether thousands should be grouped * @param int $roundingMode One of the ROUND_ constants in this class */ - public function __construct(bool $grouping = null, int $roundingMode = null) + public function __construct(bool $grouping = null, ?int $roundingMode = null) { parent::__construct(0, $grouping, $roundingMode ?? self::ROUND_DOWN); } diff --git a/src/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php b/src/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php index fe091ed3..94d3a8c3 100644 --- a/src/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php +++ b/src/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php @@ -54,9 +54,9 @@ public function __construct(string $defaultCurrency, bool $grouping = false) * @throws TransformationFailedException If the given value is not numeric or * if the value can not be transformed * - * @return string Localized money string + * @return string|null Localized money string */ - public function transform($value) + public function transform($value): ?string { if (null === $value) { return ''; @@ -81,20 +81,20 @@ public function transform($value) /** * Transforms a localized number into an integer or float. * - * @param string $value The localized value + * @param string|null $value The localized value * * @throws TransformationFailedException if the given value is not a string * or if the value can not be transformed * - * @return MoneyValue + * @return MoneyValue|null */ - public function reverseTransform($value) + public function reverseTransform($value): ?MoneyValue { - if (!is_string($value)) { - throw new TransformationFailedException('Expected a string.'); + if (null !== $value && !is_string($value)) { + throw new TransformationFailedException('Expected a string or null.'); } - if ('' === $value) { + if (null === $value || '' === $value) { return null; } @@ -158,11 +158,12 @@ private function getNumberFormatter(): \NumberFormatter * ICU cannot parse() without a currency, * and decimal doesn't include scale when 0. * - * @param string $value + * @param string $value + * @param null|string $currency * * @return string */ - private function addCurrencySymbol(string $value, string $currency = null): string + private function addCurrencySymbol(string $value, ?string $currency = null): string { $currency = $currency ?? $this->defaultCurrency; $locale = \Locale::getDefault(); @@ -205,6 +206,7 @@ private function addCurrencySymbol(string $value, string $currency = null): stri * And, decimal doesn't include scale when 0. * * @param string $value + * @param string $currency * * @return string */ diff --git a/src/Extension/Core/DataTransformer/MoneyToStringTransformer.php b/src/Extension/Core/DataTransformer/MoneyToStringTransformer.php index a6f0fb57..16540339 100644 --- a/src/Extension/Core/DataTransformer/MoneyToStringTransformer.php +++ b/src/Extension/Core/DataTransformer/MoneyToStringTransformer.php @@ -53,7 +53,7 @@ public function __construct(string $defaultCurrency) * * @return string Normalized money string */ - public function transform($value) + public function transform($value): ?string { if (null === $value) { return ''; @@ -82,15 +82,15 @@ public function transform($value) * @throws TransformationFailedException If the given value is not a string * or if the value can not be transformed * - * @return MoneyValue Normalized number + * @return MoneyValue|null Normalized number */ - public function reverseTransform($value) + public function reverseTransform($value): ?MoneyValue { - if (!is_string($value)) { - throw new TransformationFailedException('Expected a string value.'); + if (null !== $value && !is_string($value)) { + throw new TransformationFailedException('Expected a string or null.'); } - if ('' === $value) { + if (null === $value || '' === $value) { return null; } diff --git a/src/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php b/src/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php index 0631327d..1975751b 100644 --- a/src/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php +++ b/src/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php @@ -25,12 +25,7 @@ */ class NumberToLocalizedStringTransformer extends BaseNumberTransformer { - /** - * @param int $scale - * @param bool $grouping - * @param int $roundingMode - */ - public function __construct(int $scale = null, bool $grouping = null, int $roundingMode = null) + public function __construct(?int $scale = null, ?bool $grouping = null, ?int $roundingMode = null) { $this->scale = $scale; $this->grouping = $grouping ?? false; @@ -40,21 +35,21 @@ public function __construct(int $scale = null, bool $grouping = null, int $round /** * Transforms a number type into localized number. * - * @param int|float $value Number value + * @param int|float|null $value Number value * * @throws TransformationFailedException If the given value is not numeric * or if the value can not be transformed * * @return string Localized value */ - public function transform($value) + public function transform($value): string { - if (null === $value) { - return ''; + if (null !== $value && !is_numeric($value)) { + throw new TransformationFailedException('Expected a numeric or null.'); } - if (!is_numeric($value)) { - throw new TransformationFailedException('Expected a numeric.'); + if (null === $value || '' === $value) { + return ''; } $formatter = $this->getNumberFormatter(); @@ -155,11 +150,6 @@ public function reverseTransform($value) return $this->round($result); } - /** - * Returns a preconfigured \NumberFormatter instance. - * - * @return \NumberFormatter - */ private function getNumberFormatter(): \NumberFormatter { $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL); diff --git a/src/Extension/Core/DataTransformer/NumberToStringTransformer.php b/src/Extension/Core/DataTransformer/NumberToStringTransformer.php index a6a9efba..37129dc3 100644 --- a/src/Extension/Core/DataTransformer/NumberToStringTransformer.php +++ b/src/Extension/Core/DataTransformer/NumberToStringTransformer.php @@ -26,7 +26,7 @@ class NumberToStringTransformer extends BaseNumberTransformer * @param int $scale * @param int $roundingMode */ - public function __construct(int $scale = null, int $roundingMode = null) + public function __construct(int $scale = null, ?int $roundingMode = null) { $this->scale = $scale; $this->roundingMode = $roundingMode ?? self::ROUND_HALF_UP; @@ -35,21 +35,21 @@ public function __construct(int $scale = null, int $roundingMode = null) /** * Transforms a number type into number. * - * @param int|float $value Number value + * @param int|float|null $value Number value * * @throws TransformationFailedException If the given value is not numeric * or if the value can not be transformed * * @return string */ - public function transform($value) + public function transform($value): string { - if (null === $value) { - return ''; + if (null !== $value && !is_numeric($value)) { + throw new TransformationFailedException('Expected a numeric or null.'); } - if (!is_numeric($value)) { - throw new TransformationFailedException('Expected a numeric.'); + if (null === $value || '' === $value) { + return ''; } if ($value >= PHP_INT_MAX || $value <= -PHP_INT_MAX) { diff --git a/src/Field/GenericResolvedFieldType.php b/src/Field/GenericResolvedFieldType.php index ee71ac36..a5efdc51 100644 --- a/src/Field/GenericResolvedFieldType.php +++ b/src/Field/GenericResolvedFieldType.php @@ -36,7 +36,7 @@ class GenericResolvedFieldType implements ResolvedFieldType private $typeExtensions; /** - * @var GenericResolvedFieldType + * @var ResolvedFieldType|null */ private $parent; @@ -48,13 +48,13 @@ class GenericResolvedFieldType implements ResolvedFieldType /** * Constructor. * - * @param FieldType $innerType - * @param array $typeExtensions - * @param ResolvedFieldType $parent + * @param FieldType $innerType + * @param array $typeExtensions + * @param ResolvedFieldType|null $parent * * @throws UnexpectedTypeException When at least one of the given extensions is not an FieldTypeExtension */ - public function __construct(FieldType $innerType, array $typeExtensions = [], ResolvedFieldType $parent = null) + public function __construct(FieldType $innerType, array $typeExtensions = [], ?ResolvedFieldType $parent = null) { foreach ($typeExtensions as $extension) { if (!$extension instanceof FieldTypeExtension) { diff --git a/src/Input/ArrayInput.php b/src/Input/ArrayInput.php index e0316a35..ca6dde2f 100644 --- a/src/Input/ArrayInput.php +++ b/src/Input/ArrayInput.php @@ -56,7 +56,7 @@ final class ArrayInput extends AbstractInput { /** - * @var FieldValuesFactory + * @var FieldValuesFactory|null */ private $valuesFactory; diff --git a/src/Input/StringQueryInput.php b/src/Input/StringQueryInput.php index e4daf1b4..29eaa30e 100644 --- a/src/Input/StringQueryInput.php +++ b/src/Input/StringQueryInput.php @@ -138,7 +138,7 @@ final class StringQueryInput extends AbstractInput { /** - * @var FieldValuesByViewFactory + * @var FieldValuesByViewFactory|null */ private $valuesFactory; diff --git a/src/Input/XmlInput.php b/src/Input/XmlInput.php index a0915d7c..0c035947 100644 --- a/src/Input/XmlInput.php +++ b/src/Input/XmlInput.php @@ -37,7 +37,7 @@ final class XmlInput extends AbstractInput { /** - * @var FieldValuesFactory + * @var FieldValuesFactory|null */ private $valuesFactory; diff --git a/src/Test/SearchIntegrationTestCase.php b/src/Test/SearchIntegrationTestCase.php index 5f26b340..011d56ee 100644 --- a/src/Test/SearchIntegrationTestCase.php +++ b/src/Test/SearchIntegrationTestCase.php @@ -18,11 +18,11 @@ use Rollerworks\Component\Search\Extension\Core\Type\IntegerType; use Rollerworks\Component\Search\Extension\Core\Type\TextType; use Rollerworks\Component\Search\GenericFieldSetBuilder; -use Rollerworks\Component\Search\GenericSearchFactory; use Rollerworks\Component\Search\Input\ProcessorConfig; use Rollerworks\Component\Search\InputProcessor; use Rollerworks\Component\Search\SearchCondition; use Rollerworks\Component\Search\Searches; +use Rollerworks\Component\Search\SearchFactory; use Rollerworks\Component\Search\SearchFactoryBuilder; use Rollerworks\Component\Search\Tests\Input\InputProcessorTestCase; use Rollerworks\Component\Search\Value\Compare; @@ -39,7 +39,7 @@ abstract class SearchIntegrationTestCase extends TestCase protected $factoryBuilder; /** - * @var GenericSearchFactory + * @var SearchFactory */ private $searchFactory; @@ -50,7 +50,7 @@ protected function setUp() $this->factoryBuilder = Searches::createSearchFactoryBuilder(); } - protected function getFactory(): GenericSearchFactory + protected function getFactory(): SearchFactory { if (null === $this->searchFactory) { $this->factoryBuilder->addExtensions($this->getExtensions()); diff --git a/src/Util/XmlUtil.php b/src/Util/XmlUtil.php index 3855eed9..077c0ae8 100644 --- a/src/Util/XmlUtil.php +++ b/src/Util/XmlUtil.php @@ -95,7 +95,7 @@ public static function parseXml(string $content, $schemaOrCallable = null, strin } catch (\Exception $e) { $valid = false; } - } elseif (!is_array($schemaOrCallable) && is_file((string) $schemaOrCallable)) { + } elseif (!is_array($schemaOrCallable) && is_file($schemaOrCallable)) { $valid = @$dom->schemaValidate($schemaOrCallable); } else { libxml_use_internal_errors($internalErrors); diff --git a/tests/Extension/Core/ChoiceList/Factory/CachingFactoryDecoratorTest.php b/tests/Extension/Core/ChoiceList/Factory/CachingFactoryDecoratorTest.php index 8a7bc0aa..b0a704fb 100644 --- a/tests/Extension/Core/ChoiceList/Factory/CachingFactoryDecoratorTest.php +++ b/tests/Extension/Core/ChoiceList/Factory/CachingFactoryDecoratorTest.php @@ -26,12 +26,12 @@ class CachingFactoryDecoratorTest extends TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject|null */ private $decoratedFactory; /** - * @var CachingFactoryDecorator + * @var CachingFactoryDecorator|null */ private $factory; diff --git a/tests/Extension/Core/ChoiceList/Factory/DefaultChoiceListFactoryTest.php b/tests/Extension/Core/ChoiceList/Factory/DefaultChoiceListFactoryTest.php index eb8daad0..f6022b36 100644 --- a/tests/Extension/Core/ChoiceList/Factory/DefaultChoiceListFactoryTest.php +++ b/tests/Extension/Core/ChoiceList/Factory/DefaultChoiceListFactoryTest.php @@ -35,7 +35,7 @@ final class DefaultChoiceListFactoryTest extends TestCase private $list; /** - * @var DefaultChoiceListFactory + * @var DefaultChoiceListFactory|null */ private $factory; diff --git a/tests/Extension/Core/ChoiceList/Factory/PropertyAccessDecoratorTest.php b/tests/Extension/Core/ChoiceList/Factory/PropertyAccessDecoratorTest.php index df44761d..2283ce04 100644 --- a/tests/Extension/Core/ChoiceList/Factory/PropertyAccessDecoratorTest.php +++ b/tests/Extension/Core/ChoiceList/Factory/PropertyAccessDecoratorTest.php @@ -28,12 +28,12 @@ class PropertyAccessDecoratorTest extends TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject|null */ private $decoratedFactory; /** - * @var PropertyAccessDecorator + * @var PropertyAccessDecorator|null */ private $factory; diff --git a/tests/Extension/Core/ChoiceList/LazyChoiceListTest.php b/tests/Extension/Core/ChoiceList/LazyChoiceListTest.php index c48a3729..11e23616 100644 --- a/tests/Extension/Core/ChoiceList/LazyChoiceListTest.php +++ b/tests/Extension/Core/ChoiceList/LazyChoiceListTest.php @@ -24,17 +24,17 @@ class LazyChoiceListTest extends TestCase { /** - * @var LazyChoiceList + * @var LazyChoiceList|null */ private $list; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject|null */ private $loadedList; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject|null */ private $loader; diff --git a/tests/Extension/Core/ChoiceList/Loader/CallbackChoiceLoaderTest.php b/tests/Extension/Core/ChoiceList/Loader/CallbackChoiceLoaderTest.php index eb265a4f..ca83ef75 100644 --- a/tests/Extension/Core/ChoiceList/Loader/CallbackChoiceLoaderTest.php +++ b/tests/Extension/Core/ChoiceList/Loader/CallbackChoiceLoaderTest.php @@ -23,27 +23,27 @@ final class CallbackChoiceLoaderTest extends TestCase { /** - * @var CallbackChoiceLoader + * @var CallbackChoiceLoader|null */ private static $loader; /** - * @var callable + * @var callable|null */ private static $value; /** - * @var array + * @var array|null */ private static $choices; /** - * @var string[] + * @var string[]|null */ private static $choiceValues; /** - * @var LazyChoiceList + * @var LazyChoiceList|null */ private static $lazyChoiceList; diff --git a/tests/Extension/Core/DataTransformer/ChoiceToValueTransformerTest.php b/tests/Extension/Core/DataTransformer/ChoiceToValueTransformerTest.php index f4bad159..2b6eda0d 100644 --- a/tests/Extension/Core/DataTransformer/ChoiceToValueTransformerTest.php +++ b/tests/Extension/Core/DataTransformer/ChoiceToValueTransformerTest.php @@ -24,12 +24,12 @@ final class ChoiceToValueTransformerTest extends TestCase { /** - * @var ChoiceToValueTransformer + * @var ChoiceToValueTransformer|null */ private $transformer; /** - * @var ChoiceToValueTransformer + * @var ChoiceToValueTransformer|null */ private $transformerWithNull; diff --git a/tests/LazyExtensionTest.php b/tests/Extension/LazyExtensionTest.php similarity index 98% rename from tests/LazyExtensionTest.php rename to tests/Extension/LazyExtensionTest.php index 89438a47..7c7e424f 100644 --- a/tests/LazyExtensionTest.php +++ b/tests/Extension/LazyExtensionTest.php @@ -11,7 +11,7 @@ * with this source code in the file LICENSE. */ -namespace Rollerworks\Component\Search\Tests; +namespace Rollerworks\Component\Search\Tests\Extension; use PHPUnit\Framework\TestCase; use Rollerworks\Component\Search\Exception\InvalidArgumentException; diff --git a/tests/ResolvedFieldTypeTest.php b/tests/Field/ResolvedFieldTypeTest.php similarity index 99% rename from tests/ResolvedFieldTypeTest.php rename to tests/Field/ResolvedFieldTypeTest.php index 19a3cb16..47552f45 100644 --- a/tests/ResolvedFieldTypeTest.php +++ b/tests/Field/ResolvedFieldTypeTest.php @@ -11,7 +11,7 @@ * with this source code in the file LICENSE. */ -namespace Rollerworks\Component\Search\Tests; +namespace Rollerworks\Component\Search\Tests\Field; use PHPUnit\Framework\TestCase; use Rollerworks\Component\Search\Field\AbstractFieldType; diff --git a/tests/SearchFieldTest.php b/tests/Field/SearchFieldTest.php similarity index 98% rename from tests/SearchFieldTest.php rename to tests/Field/SearchFieldTest.php index 6e6e9979..09233e41 100644 --- a/tests/SearchFieldTest.php +++ b/tests/Field/SearchFieldTest.php @@ -11,7 +11,7 @@ * with this source code in the file LICENSE. */ -namespace Rollerworks\Component\Search\Tests; +namespace Rollerworks\Component\Search\Tests\Field; use PHPUnit\Framework\TestCase; use Rollerworks\Component\Search\DataTransformer; @@ -27,7 +27,7 @@ final class SearchFieldTest extends TestCase { /** - * @var \Rollerworks\Component\Search\ResolvedFieldType + * @var ResolvedFieldType */ private $resolvedType; diff --git a/tests/TypeRegistryTest.php b/tests/Field/TypeRegistryTest.php similarity index 98% rename from tests/TypeRegistryTest.php rename to tests/Field/TypeRegistryTest.php index 56ad6619..e26d98c2 100644 --- a/tests/TypeRegistryTest.php +++ b/tests/Field/TypeRegistryTest.php @@ -11,7 +11,7 @@ * with this source code in the file LICENSE. */ -namespace Rollerworks\Component\Search\Tests; +namespace Rollerworks\Component\Search\Tests\Field; use PHPUnit\Framework\TestCase; use Prophecy\Argument; diff --git a/tests/Input/FieldValuesFactoryTest.php b/tests/Input/FieldValuesFactoryTest.php index 579097a7..f104ec25 100644 --- a/tests/Input/FieldValuesFactoryTest.php +++ b/tests/Input/FieldValuesFactoryTest.php @@ -22,7 +22,6 @@ use Rollerworks\Component\Search\Extension\Core\Type\IntegerType; use Rollerworks\Component\Search\Extension\Core\Type\TextType; use Rollerworks\Component\Search\Field\FieldConfig; -use Rollerworks\Component\Search\Field\SearchField; use Rollerworks\Component\Search\Input\FieldValuesFactory; use Rollerworks\Component\Search\Input\NullValidator; use Rollerworks\Component\Search\Input\Validator; @@ -503,7 +502,7 @@ private function createValuesFactory(Validator $validator = null, int $valuesLim return new FieldValuesFactory($this->errorList, $validator, $valuesLimit); } - private function createField(string $type = null, array $options = []): SearchField + private function createField(string $type = null, array $options = []): FieldConfig { return $this->getFactory()->createField('field-name', $type ?? TextType::class, $options); } @@ -543,7 +542,7 @@ private function assertProducedErrors(array $errors) self::assertEquals($errors, $errorsList); } - private function createFullySupportedField(): SearchField + private function createFullySupportedField(): FieldConfig { $field = $this->createField(); $field->setValueTypeSupport(Range::class, true); diff --git a/tests/ValuesBagTest.php b/tests/Value/ValuesBagTest.php similarity index 98% rename from tests/ValuesBagTest.php rename to tests/Value/ValuesBagTest.php index b92b63d8..a6e75fe3 100644 --- a/tests/ValuesBagTest.php +++ b/tests/Value/ValuesBagTest.php @@ -11,7 +11,7 @@ * with this source code in the file LICENSE. */ -namespace Rollerworks\Component\Search\Tests; +namespace Rollerworks\Component\Search\Tests\Value; use PHPUnit\Framework\TestCase; use Rollerworks\Component\Search\Value\Compare; diff --git a/tests/ValuesGroupTest.php b/tests/Value/ValuesGroupTest.php similarity index 98% rename from tests/ValuesGroupTest.php rename to tests/Value/ValuesGroupTest.php index 8fd8267c..386f376d 100644 --- a/tests/ValuesGroupTest.php +++ b/tests/Value/ValuesGroupTest.php @@ -11,7 +11,7 @@ * with this source code in the file LICENSE. */ -namespace Rollerworks\Component\Search\Tests; +namespace Rollerworks\Component\Search\Tests\Value; use PHPUnit\Framework\TestCase; use Rollerworks\Component\Search\Value\ValuesBag;