Skip to content

Commit

Permalink
replace remaining builtin functions calls by PSL
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed May 19, 2021
1 parent 0bb5acb commit e79dae1
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 145 deletions.
10 changes: 6 additions & 4 deletions src/Changes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, Change>
*/
final class Changes implements IteratorAggregate, Countable
{
/** @var Change[] */
Expand Down Expand Up @@ -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));
}
}
7 changes: 3 additions & 4 deletions src/DetectChanges/BCBreak/ClassBased/ConstantChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/DetectChanges/BCBreak/ClassBased/PropertyChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/DetectChanges/Variance/TypeIsContravariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -70,9 +69,6 @@ public function __invoke(
return $typeReflectionClass->implementsInterface($comparedTypeAsString);
}

return ArrayHelpers::stringArrayContainsString(
$comparedTypeAsString,
$typeReflectionClass->getParentClassNames()
);
return Iter\contains($typeReflectionClass->getParentClassNames(), $comparedTypeAsString);
}
}
12 changes: 4 additions & 8 deletions src/DetectChanges/Variance/TypeIsCovariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -81,9 +80,6 @@ public function __invoke(
return $comparedTypeReflectionClass->implementsInterface($typeAsString);
}

return ArrayHelpers::stringArrayContainsString(
$typeAsString,
$comparedTypeReflectionClass->getParentClassNames()
);
return Iter\contains($comparedTypeReflectionClass->getParentClassNames(), $typeAsString);
}
}
37 changes: 18 additions & 19 deletions src/Formatter/MarkdownPipedToSymfonyConsoleFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string>
*/
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";
}
);
}
}
28 changes: 0 additions & 28 deletions src/Support/ArrayHelpers.php

This file was deleted.

70 changes: 0 additions & 70 deletions test/unit/Support/ArrayHelpersTest.php

This file was deleted.

0 comments on commit e79dae1

Please sign in to comment.