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

refactor: fix PHPStan errors #9125

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
546 changes: 0 additions & 546 deletions phpstan-baseline.php

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions phpstan-bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

require __DIR__ . '/system/Test/bootstrap.php';

if (! defined('OCI_COMMIT_ON_SUCCESS')) {
Expand Down
13 changes: 11 additions & 2 deletions system/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function initialize(Autoload $config, Modules $modules)
$this->files = $config->files;
}

if (isset($config->helpers)) {
if ($config->helpers !== []) {
$this->helpers = [...$this->helpers, ...$config->helpers];
}

Expand Down Expand Up @@ -367,6 +367,9 @@ public function sanitizeFilename(string $filename): string
return $cleanFilename;
}

/**
* @param array{only?: list<string>, exclude?: list<string>} $composerPackages
*/
private function loadComposerNamespaces(ClassLoader $composer, array $composerPackages): void
{
$namespacePaths = $composer->getPrefixesPsr4();
Expand All @@ -380,7 +383,13 @@ private function loadComposerNamespaces(ClassLoader $composer, array $composerPa
}
}

if (! method_exists(InstalledVersions::class, 'getAllRawData')) {
preg_match(
'/\s*"plugin-api-version": "(?\'version\'\d+\.\d+\.\d+)"/m',
file_get_contents(__DIR__ . '/../../composer.lock'),
$matches
);

if (version_compare($matches['version'], '2.0.14', '<')) {
Comment on lines -383 to +392
Copy link
Member

Choose a reason for hiding this comment

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

Why did you change the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fix phpstan error:
Call to function method_exists() with 'Composer\InstalledVersions' and 'getAllRawData' will always evaluate to true.
But I think, that better way will be to remove this check and check composer version during composer install and composer update...

Copy link
Member

Choose a reason for hiding this comment

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

Okay, I got why. But your code seems to take time more.
The PHPStan error is inevitable. Because we use the latest composer command.
So I think the error should be suppressed by @phpstan-ignore-next-line or @phpstan-ignore-line.

throw new RuntimeException(
'Your Composer version is too old.'
. ' Please update Composer (run `composer self-update`) to v2.0.14 or later'
Expand Down
22 changes: 12 additions & 10 deletions system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ public function getClassname(string $file): string

if ((isset($tokens[$i - 2][1]) && ($tokens[$i - 2][1] === 'phpnamespace' || $tokens[$i - 2][1] === 'namespace')) || ($dlm && $tokens[$i - 1][0] === T_NS_SEPARATOR && $token[0] === T_STRING)) {
if (! $dlm) {
$namespace = 0;
$namespace = '';
Copy link
Member

Choose a reason for hiding this comment

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

I don't know why the original code is $namespace = 0;.
If $namespace is set to 0, is the case is just a bug?

Please make another PR to fix this issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After changing 151 line in this file it's failing:

  1. CodeIgniter\Autoloader\FileLocatorCachedTest::testGetClassNameFromClassFile
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'CodeIgniter\Autoloader\FileLocatorTest'
    +'0\CodeIgniter\Autoloader\FileLocatorTest'

}
if (isset($token[1])) {
$namespace = $namespace ? $namespace . '\\' . $token[1] : $token[1];
$namespace = $namespace !== '' ? $namespace . '\\' . $token[1] : $token[1];
$dlm = true;
}
} elseif ($dlm && ($token[0] !== T_NS_SEPARATOR) && ($token[0] !== T_STRING)) {
Expand Down Expand Up @@ -194,8 +194,9 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp =

foreach ($this->getNamespaces() as $namespace) {
if (isset($namespace['path']) && is_file($namespace['path'] . $path)) {
$fullPath = $namespace['path'] . $path;
$fullPath = realpath($fullPath) ?: $fullPath;
$fullPath = ($realPath = realpath($namespace['path'] . $path)) !== false
? $realPath
: ($namespace['path'] . $path);

if ($prioritizeApp) {
$foundPaths[] = $fullPath;
Expand Down Expand Up @@ -272,14 +273,14 @@ protected function getNamespaces()
*/
public function findQualifiedNameFromPath(string $path)
{
$path = realpath($path) ?: $path;
$path = ($realPath = realpath($path)) !== false ? $realPath : $path;

if (! is_file($path)) {
return false;
}

foreach ($this->getNamespaces() as $namespace) {
$namespace['path'] = realpath($namespace['path']) ?: $namespace['path'];
$namespace['path'] = ($realPath = realpath($namespace['path'])) !== false ? $realPath : $namespace['path'];

if ($namespace['path'] === '') {
continue;
Expand Down Expand Up @@ -331,8 +332,9 @@ public function listFiles(string $path): array
helper('filesystem');

foreach ($this->getNamespaces() as $namespace) {
$fullPath = $namespace['path'] . $path;
$fullPath = realpath($fullPath) ?: $fullPath;
$fullPath = ($realPath = realpath($namespace['path'] . $path)) !== false
? $realPath
: ($namespace['path'] . $path);

if (! is_dir($fullPath)) {
continue;
Expand Down Expand Up @@ -366,7 +368,7 @@ public function listNamespaceFiles(string $prefix, string $path): array
// autoloader->getNamespace($prefix) returns an array of paths for that namespace
foreach ($this->autoloader->getNamespace($prefix) as $namespacePath) {
$fullPath = rtrim($namespacePath, '/') . '/' . $path;
$fullPath = realpath($fullPath) ?: $fullPath;
$fullPath = ($realPath = realpath($fullPath)) !== false ? $realPath : $fullPath;

if (! is_dir($fullPath)) {
continue;
Expand All @@ -393,7 +395,7 @@ public function listNamespaceFiles(string $prefix, string $path): array
protected function legacyLocate(string $file, ?string $folder = null)
{
$path = APPPATH . ($folder === null ? $file : $folder . '/' . $file);
$path = realpath($path) ?: $path;
$path = ($realPath = realpath($path)) !== false ? $realPath : $path;

if (is_file($path)) {
return $path;
Expand Down
51 changes: 51 additions & 0 deletions system/Autoloader/FileLocatorCached.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public function deleteCache(): void
$this->cacheHandler->delete($this->cacheKey);
}

/**
* Find the qualified name of a file according to
* the namespace of the first matched namespace path.
*
* @return false|string The qualified name or false if the path is not found
*/
public function findQualifiedNameFromPath(string $path): false|string
{
if (isset($this->cache['findQualifiedNameFromPath'][$path])) {
Expand All @@ -100,6 +106,9 @@ public function findQualifiedNameFromPath(string $path): false|string
return $classname;
}

/**
* Examines a file and returns the fully qualified class name.
*/
public function getClassname(string $file): string
{
if (isset($this->cache['getClassname'][$file])) {
Expand All @@ -114,6 +123,21 @@ public function getClassname(string $file): string
return $classname;
}

/**
* Searches through all of the defined namespaces looking for a file.
* Returns an array of all found locations for the defined file.
*
* Example:
*
* $locator->search('Config/Routes.php');
* // Assuming PSR4 namespaces include foo and bar, might return:
* [
* 'app/Modules/foo/Config/Routes.php',
* 'app/Modules/bar/Config/Routes.php',
* ]
*
* @return list<string> List of file paths
*/
public function search(string $path, string $ext = 'php', bool $prioritizeApp = true): array
{
if (isset($this->cache['search'][$path][$ext][$prioritizeApp])) {
Expand All @@ -128,6 +152,12 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp =
return $foundPaths;
}

/**
* Scans the defined namespaces, returning a list of all files
* that are contained within the subpath specified by $path.
*
* @return list<string> List of file paths
*/
public function listFiles(string $path): array
{
if (isset($this->cache['listFiles'][$path])) {
Expand All @@ -142,6 +172,12 @@ public function listFiles(string $path): array
return $files;
}

/**
* Scans the provided namespace, returning a list of all files
* that are contained within the sub path specified by $path.
*
* @return list<string> List of file paths
*/
public function listNamespaceFiles(string $prefix, string $path): array
{
if (isset($this->cache['listNamespaceFiles'][$prefix][$path])) {
Expand All @@ -156,6 +192,21 @@ public function listNamespaceFiles(string $prefix, string $path): array
return $files;
}

/**
* Attempts to locate a file by examining the name for a namespace
* and looking through the PSR-4 namespaced files that we know about.
*
* @param string $file The relative file path or namespaced file to
* locate. If not namespaced, search in the app
* folder.
* @param non-empty-string|null $folder The folder within the namespace that we should
* look for the file. If $file does not contain
* this value, it will be appended to the namespace
* folder.
* @param string $ext The file extension the file should have.
*
* @return false|string The path to the file, or false if not found.
*/
public function locateFile(string $file, ?string $folder = null, string $ext = 'php'): false|string
{
if (isset($this->cache['locateFile'][$file][$folder][$ext])) {
Expand Down
2 changes: 2 additions & 0 deletions system/Autoloader/FileLocatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function getClassname(string $file): string;
* 'app/Modules/foo/Config/Routes.php',
* 'app/Modules/bar/Config/Routes.php',
* ]
*
* @return list<string> List of file paths
*/
public function search(string $path, string $ext = 'php', bool $prioritizeApp = true): array;

Expand Down
Loading
Loading