diff --git a/src/Changes.php b/src/Changes.php index f4f41cdd..23bd5bc5 100644 --- a/src/Changes.php +++ b/src/Changes.php @@ -7,11 +7,13 @@ use Countable; use Generator; use IteratorAggregate; +use Psl\Dict; +use Psl\Iter; use Traversable; -use function count; -use function iterator_to_array; - +/** + * @implements IteratorAggregate + */ final class Changes implements IteratorAggregate, Countable { /** @var Change[] */ @@ -99,6 +101,6 @@ public function getIterator(): iterable public function count(): int { - return count(iterator_to_array($this)); + return Iter\count(Dict\from_iterable($this)); } } diff --git a/src/DetectChanges/BCBreak/ClassBased/ConstantChanged.php b/src/DetectChanges/BCBreak/ClassBased/ConstantChanged.php index 76c083c8..5f5e2e21 100644 --- a/src/DetectChanges/BCBreak/ClassBased/ConstantChanged.php +++ b/src/DetectChanges/BCBreak/ClassBased/ConstantChanged.php @@ -4,15 +4,14 @@ namespace Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased; +use Psl\Dict; +use Psl\Vec; use Roave\BackwardCompatibility\Change; use Roave\BackwardCompatibility\Changes; use Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassConstantBased\ClassConstantBased; use Roave\BetterReflection\Reflection\ReflectionClass; use Roave\BetterReflection\Reflection\ReflectionClassConstant; -use function array_intersect_key; -use function array_keys; - final class ConstantChanged implements ClassBased { private ClassConstantBased $checkConstant; @@ -38,7 +37,7 @@ public function __invoke(ReflectionClass $fromClass, ReflectionClass $toClass): */ private function checkSymbols(array $from, array $to): iterable { - foreach (array_keys(array_intersect_key($from, $to)) as $name) { + foreach (Vec\keys(Dict\intersect_by_key($from, $to)) as $name) { yield from $this->checkConstant->__invoke($from[$name], $to[$name]); } } diff --git a/src/DetectChanges/BCBreak/ClassBased/PropertyChanged.php b/src/DetectChanges/BCBreak/ClassBased/PropertyChanged.php index 7893add4..410e2138 100644 --- a/src/DetectChanges/BCBreak/ClassBased/PropertyChanged.php +++ b/src/DetectChanges/BCBreak/ClassBased/PropertyChanged.php @@ -4,15 +4,14 @@ namespace Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased; +use Psl\Dict; +use Psl\Vec; use Roave\BackwardCompatibility\Change; use Roave\BackwardCompatibility\Changes; use Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased\PropertyBased; use Roave\BetterReflection\Reflection\ReflectionClass; use Roave\BetterReflection\Reflection\ReflectionProperty; -use function array_intersect_key; -use function array_keys; - final class PropertyChanged implements ClassBased { private PropertyBased $checkProperty; @@ -38,7 +37,7 @@ public function __invoke(ReflectionClass $fromClass, ReflectionClass $toClass): */ private function checkSymbols(array $from, array $to): iterable { - foreach (array_keys(array_intersect_key($from, $to)) as $name) { + foreach (Vec\keys(Dict\intersect_by_key($from, $to)) as $name) { yield from $this->checkProperty->__invoke($from[$name], $to[$name]); } } diff --git a/src/DetectChanges/Variance/TypeIsContravariant.php b/src/DetectChanges/Variance/TypeIsContravariant.php index 185e74b5..31ba0dff 100644 --- a/src/DetectChanges/Variance/TypeIsContravariant.php +++ b/src/DetectChanges/Variance/TypeIsContravariant.php @@ -4,11 +4,10 @@ namespace Roave\BackwardCompatibility\DetectChanges\Variance; -use Roave\BackwardCompatibility\Support\ArrayHelpers; +use Psl\Iter; +use Psl\Str; use Roave\BetterReflection\Reflection\ReflectionType; -use function strtolower; - /** * This is a simplistic contravariant type check. A more appropriate approach would be to * have a `$type->includes($otherType)` check with actual types represented as value objects, @@ -37,7 +36,7 @@ public function __invoke( $typeAsString = $type->__toString(); $comparedTypeAsString = $comparedType->__toString(); - if (strtolower($typeAsString) === strtolower($comparedTypeAsString)) { + if (Str\lowercase($typeAsString) === Str\lowercase($comparedTypeAsString)) { return true; } @@ -70,9 +69,6 @@ public function __invoke( return $typeReflectionClass->implementsInterface($comparedTypeAsString); } - return ArrayHelpers::stringArrayContainsString( - $comparedTypeAsString, - $typeReflectionClass->getParentClassNames() - ); + return Iter\contains($typeReflectionClass->getParentClassNames(), $comparedTypeAsString); } } diff --git a/src/DetectChanges/Variance/TypeIsCovariant.php b/src/DetectChanges/Variance/TypeIsCovariant.php index c2f0eea3..4c819c44 100644 --- a/src/DetectChanges/Variance/TypeIsCovariant.php +++ b/src/DetectChanges/Variance/TypeIsCovariant.php @@ -4,12 +4,11 @@ namespace Roave\BackwardCompatibility\DetectChanges\Variance; -use Roave\BackwardCompatibility\Support\ArrayHelpers; +use Psl\Iter; +use Psl\Str; use Roave\BetterReflection\Reflection\ReflectionType; use Traversable; -use function strtolower; - /** * This is a simplistic covariant type check. A more appropriate approach would be to * have a `$type->includes($otherType)` check with actual types represented as value objects, @@ -39,7 +38,7 @@ public function __invoke( $typeAsString = $type->__toString(); $comparedTypeAsString = $comparedType->__toString(); - if (strtolower($typeAsString) === strtolower($comparedTypeAsString)) { + if (Str\lowercase($typeAsString) === Str\lowercase($comparedTypeAsString)) { return true; } @@ -81,9 +80,6 @@ public function __invoke( return $comparedTypeReflectionClass->implementsInterface($typeAsString); } - return ArrayHelpers::stringArrayContainsString( - $typeAsString, - $comparedTypeReflectionClass->getParentClassNames() - ); + return Iter\contains($comparedTypeReflectionClass->getParentClassNames(), $typeAsString); } } diff --git a/src/Formatter/MarkdownPipedToSymfonyConsoleFormatter.php b/src/Formatter/MarkdownPipedToSymfonyConsoleFormatter.php index 85e9d95f..2932c555 100644 --- a/src/Formatter/MarkdownPipedToSymfonyConsoleFormatter.php +++ b/src/Formatter/MarkdownPipedToSymfonyConsoleFormatter.php @@ -4,17 +4,12 @@ namespace Roave\BackwardCompatibility\Formatter; +use Psl\Str; +use Psl\Vec; use Roave\BackwardCompatibility\Change; use Roave\BackwardCompatibility\Changes; use Symfony\Component\Console\Output\OutputInterface; -use function array_filter; -use function array_map; -use function implode; -use function iterator_to_array; -use function str_replace; -use function trim; - final class MarkdownPipedToSymfonyConsoleFormatter implements OutputFormatter { private OutputInterface $output; @@ -26,41 +21,45 @@ public function __construct(OutputInterface $output) public function write(Changes $changes): void { - $arrayOfChanges = iterator_to_array($changes); + $arrayOfChanges = Vec\values($changes); $this->output->writeln( "# Added\n" - . implode('', $this->convertFilteredChangesToMarkdownBulletList( + . Str\join($this->convertFilteredChangesToMarkdownBulletList( static function (Change $change): bool { return $change->isAdded(); }, ...$arrayOfChanges - )) + ), '') . "\n# Changed\n" - . implode('', $this->convertFilteredChangesToMarkdownBulletList( + . Str\join($this->convertFilteredChangesToMarkdownBulletList( static function (Change $change): bool { return $change->isChanged(); }, ...$arrayOfChanges - )) + ), '') . "\n# Removed\n" - . implode('', $this->convertFilteredChangesToMarkdownBulletList( + . Str\join($this->convertFilteredChangesToMarkdownBulletList( static function (Change $change): bool { return $change->isRemoved(); }, ...$arrayOfChanges - )) + ), '') ); } - /** @return string[] */ + /** + * @param callable(Change): bool $filterFunction + * + * @return list + */ private function convertFilteredChangesToMarkdownBulletList(callable $filterFunction, Change ...$changes): array { - return array_map( + return Vec\map( + Vec\filter($changes, $filterFunction), static function (Change $change): string { - return ' - ' . str_replace(['ADDED: ', 'CHANGED: ', 'REMOVED: '], '', trim($change->__toString())) . "\n"; - }, - array_filter($changes, $filterFunction) + return ' - ' . Str\replace_every(Str\trim($change->__toString()), ['ADDED: ' => '', 'CHANGED: ' => '', 'REMOVED: ' => '']) . "\n"; + } ); } } diff --git a/src/Support/ArrayHelpers.php b/src/Support/ArrayHelpers.php deleted file mode 100644 index f11dc9fe..00000000 --- a/src/Support/ArrayHelpers.php +++ /dev/null @@ -1,28 +0,0 @@ -|bool>> - * - * @psalm-return array, 2: bool}> - */ - public function stringArrayContainsStringValidValues(): array - { - return [ - [ - '', - [], - false, - ], - [ - '', - [''], - true, - ], - [ - '0', - [''], - false, - ], - [ - '', - ['0'], - false, - ], - [ - 'foo', - ['foo', 'bar', 'baz'], - true, - ], - [ - 'foo', - ['bar', 'baz'], - false, - ], - [ - 'foo', - ['foo', 'foo', 'foo'], - true, - ], - ]; - } -}