-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
Versions:
- laravel-modules Version: 12.0.4
- Laravel Version: 12.25
- PHP Version: 8.4
- PHPStan Version: 2.1.22
- Larastan: 3.6.1
- Rector: 2.1.4
- Laravel Rector: 2.0.6
Description:
I'm starting the process of modifying our module directory structure to mirror how Laravel Modules 12 prefers them to be. (app directory, Models instead of Entities, etc.) During this process, I'm updating each module's Service Provider as well, using a newly-created temporary module's Service Provider as a template.
Everything is going swimmingly. I'm only running into one problem whenever I run PHPStan. It's currently set to Level 8 and the error I get is Binary operation "." between non-falsy-string and list<string>|string results in an error. This is in reference to the $segments assignment line in the registerConfig method:
protected function registerConfig(): void
{
$configPath = module_path($this->name, config('modules.paths.generator.config.path'));
if (is_dir($configPath)) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$config = str_replace($configPath . DIRECTORY_SEPARATOR, '', $file->getPathname());
$config_key = str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $config);
$segments = explode('.', $this->nameLower . '.' . $config_key); // Here is where PHPStan is throwing the error.
// Remove duplicated adjacent segments
$normalized = [];
foreach ($segments as $segment) {
if (end($normalized) !== $segment) {
$normalized[] = $segment;
}
}
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
$this->publishes([$file->getPathname() => config_path($config)], 'config');
$this->merge_config_from($file->getPathname(), $key);
}
}
}
}I can certainly add this anomaly to our phpstan-baseline.neon file so that it is ignored from now on, but it seems like there should be a way to prevent this from happening in the first place.
And I can see why PHPStan doesn't like it. str_replace can return a string or an array, so we would just need to convince it that the str_replace for $config_key will always be a string, right? Unfortunately, I've tried casting it with (string), but that isn't enough, because Stan then says that you cannot cast an array to a string.
This seems like a bug, albeit a minor one that only PHPStan would care about. Still, should this be fixed somehow? I'd gladly make a pull request myself if I knew the best way to deal with it, but I'm coming up blank right now.
Steps To Reproduce:
- Run
phpstan analyzewhile PHPStan's config is set to Level 8. (Although that particular check only comes into play at Level 2, I think.)