Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,4 @@ parameters:
message: '#Parameters should use "PhpParser\\Node\\Stmt\\ClassMethod" types as the only types passed to this method#'

# known enum from strings
- '#Method Rector\\Core\\Php\\PolyfillPackagesProvider\:\:filterPolyfillPackages\(\) should return array(.*?) but returns array<string, string>#'
- '#Method Rector\\Core\\Php\\PolyfillPackagesProvider\:\:filterPolyfillPackages\(\) should return array\<.*\> but returns array<int, non\-falsy\-string>#'
20 changes: 11 additions & 9 deletions src/Php/PolyfillPackagesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
final class PolyfillPackagesProvider
{
/**
* @var array<PolyfillPackage::*>
* @var null|array<int, PolyfillPackage::*>
*/
private array $cachedPolyfillPackages = [];
private null|array $cachedPolyfillPackages = null;

/**
* @return array<PolyfillPackage::*>
* @return array<int, PolyfillPackage::*>
*/
public function provide(): array
{
Expand All @@ -27,12 +27,14 @@ public function provide(): array
return SimpleParameterProvider::provideArrayParameter(Option::POLYFILL_PACKAGES);
}

$projectComposerJson = getcwd() . '/composer.json';
if (! file_exists($projectComposerJson)) {
return [];
// already cached, even only empty array
if ($this->cachedPolyfillPackages !== null) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here compare no null = already cached.

return $this->cachedPolyfillPackages;
}

if ($this->cachedPolyfillPackages !== []) {
$projectComposerJson = getcwd() . '/composer.json';
if (! file_exists($projectComposerJson)) {
$this->cachedPolyfillPackages = [];
return $this->cachedPolyfillPackages;
}

Expand All @@ -46,11 +48,11 @@ public function provide(): array

/**
* @param array<string, string> $require
* @return array<PolyfillPackage::*>
* @return array<int, PolyfillPackage::*>
*/
private function filterPolyfillPackages(array $require): array
{
return array_filter($require, static fn (string $packageName): bool => ! str_starts_with(
return array_filter(array_keys($require), static fn (string $packageName): bool => str_starts_with(

@samsonasik samsonasik Dec 25, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here only key is needed, when we define in composer.json

"require": {
    "symfony/polyfill-php80" => "*"
}

then, filter the str_starts_with() one instead, so it get data:

array('symfony/polyfill-php80')

to compare with in_array() on PhpVersionedFilter

$polyfillPackageNames = $this->polyfillPackagesProvider->provide();
if (in_array($rector->providePolyfillPackage(), $polyfillPackageNames, true)) {
$activeRectors[] = $rector;
continue;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$packageName,
'symfony/polyfill-'
));
Expand Down