Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase PHPStan rule level to 7 #733

Merged
merged 3 commits into from
Sep 18, 2023

Conversation

theimbender
Copy link
Contributor

Resolves #659 by increasing PHPStan rule level to 7 to account for possible issues with union types. I think I've followed the logic correctly, but could definitely use feedback on whether there should be additional exceptions or if there are better ways of handling these.

@theimbender theimbender force-pushed the feat-phpstan-level-7 branch 3 times, most recently from 5a84df7 to d758edc Compare September 17, 2023 20:02
Comment on lines 122 to 123
$hasedPath = $includeArchiveChecksum ? hash_file('sha1', $path) : false;
$package->setDistSha1Checksum(false !== $hasedPath ? $hasedPath : null);
Copy link
Contributor

Choose a reason for hiding this comment

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

hash_file will never return a falsy string ('' or '0')

Suggested change
$hasedPath = $includeArchiveChecksum ? hash_file('sha1', $path) : false;
$package->setDistSha1Checksum(false !== $hasedPath ? $hasedPath : null);
$package->setDistSha1Checksum($includeArchiveChecksum ? (hash_file('sha1', $path) ?: null) : null);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The return type for hash_file is string|false and this rule level disallows short ternary operators, I'll tweak this a little though.

$baseUrlLength = strlen($baseUrl);

foreach ($rootConfig['providers'] as $package => $provider) {
$file = str_replace(['%package%', '%hash%'], [$package, $provider['sha256']], $rootConfig['providers-url']);
$file = (string) str_replace(['%package%', '%hash%'], [$package, $provider['sha256']], $rootConfig['providers-url']);
Copy link
Contributor

Choose a reason for hiding this comment

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

If you enforce the type of the input, the type of str_replace output should be correct.

Suggested change
$file = (string) str_replace(['%package%', '%hash%'], [$package, $provider['sha256']], $rootConfig['providers-url']);
$file = str_replace(['%package%', '%hash%'], [$package, $provider['sha256']], (string) $rootConfig['providers-url']);

Comment on lines +595 to +597
if ($repository instanceof RepositoryInterface) {
$repositorySet->addRepository($repository);
}
Copy link
Contributor

@GromNaN GromNaN Sep 17, 2023

Choose a reason for hiding this comment

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

This looks like an actual bugfix. But I don't think this is possible.

@@ -577,15 +584,17 @@ private function matchAddr($addr1, $addr2, $len = 0, $chunklen = 32): bool
}

/**
* @param RepositoryInterface[] $repositories
* @param array<RepositoryInterface|ConfigurableRepositoryInterface> $repositories
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be a union type. I don't see any class that implement ConfigurableRepositoryInterface without RepositoryInterface.

Suggested change
* @param array<RepositoryInterface|ConfigurableRepositoryInterface> $repositories
* @param array<RepositoryInterface&ConfigurableRepositoryInterface> $repositories

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know if we want to change this from union to intersection types, when I do that there are several branches that would always evaluate to true or become unreachable, and some methods that use one of those for a parameter type start throwing errors as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok then, feel free to mark as resolved.

}
);

return array_values($links);
}

/**
* @param RepositoryInterface[] $repositories
* @param array<RepositoryInterface|ConfigurableRepositoryInterface> $repositories

This comment was marked as outdated.

Comment on lines +122 to +123
$hasedPath = hash_file('sha1', $path);
$package->setDistSha1Checksum($includeArchiveChecksum ? (is_string($hasedPath) ? $hasedPath : null) : null);
Copy link
Member

Choose a reason for hiding this comment

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

Nitpicking, but I think "hased" is a typo?

@alcohol alcohol force-pushed the feat-phpstan-level-7 branch from 96e16f5 to b4bc345 Compare September 18, 2023 08:33
@alcohol alcohol merged commit 1a34986 into composer:main Sep 18, 2023
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Increase PHPStan to level 7
3 participants