Skip to content

Commit

Permalink
Merge pull request #19 from kirschbaum-development/phpstan-max
Browse files Browse the repository at this point in the history
Brings `phpstan` to level `max`
  • Loading branch information
brandonferens authored Oct 22, 2024
2 parents a15b7d4 + 5933851 commit 3897980
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 54 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,61 @@ jobs:
- name: Execute tests
run: composer pest

pint:
runs-on: ubuntu-24.04
timeout-minutes: 5

name: Pint Style Check
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-composer-${{ hashFiles('composer.json') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
tools: composer:v2

- name: Install dependencies
run: |
composer install --no-interaction --no-suggest --dev
composer dump
- name: Execute Pint
run: composer pint-check

larastan:
runs-on: ubuntu-24.04
timeout-minutes: 5

name: Static Analysis
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-composer-${{ hashFiles('composer.json') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
tools: composer:v2

- name: Install dependencies
run: |
composer install --no-interaction --no-suggest --dev
composer dump
- name: Execute Larastan
run: composer larastan
10 changes: 5 additions & 5 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ includes:

parameters:
paths:
- config/
- src/
- config
- src

level: 8
level: max

ignoreErrors:
- '#Access to an undefined property UnitEnum::\$value.#'
checkMissingIterableValueType: true
treatPhpDocTypesAsCertain: false
11 changes: 6 additions & 5 deletions src/Commands/GenerateEnumsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,22 @@ public function handle(): int
/**
* Gather all enum namespaces for searching.
*
* @return Collection<int, class-string>
* @return Collection<int,class-string<\UnitEnum>>
*/
protected function enums(): Collection
{
return DiscoverEnums::within(app_path(config('paragon.enums.paths.php')))
return DiscoverEnums::within(app_path(config()->string('paragon.enums.paths.php')))
->reject(function ($enum) {
if (! enum_exists($enum)) {
return true;
}

$reflector = new ReflectionEnum($enum);

$paths = Arr::map(Arr::wrap(config('paragon.enums.paths.ignore')), function ($path) {
return Str::finish(app_path($path), '/');
});
$paths = Arr::map(
Arr::wrap(config('paragon.enums.paths.ignore')),
fn (string $path): string => Str::finish(app_path($path), '/'),
);

return $reflector->getAttributes(IgnoreParagon::class)
|| Str::startsWith((string) $reflector->getFileName(), $paths);
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/MakeEnumMethodCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ protected function promptForMissingArgumentsUsing(): array
/**
* Build the file with the given name.
*
* @param string $name
*
* @throws Exception
* @throws FileNotFoundException
*/
Expand All @@ -87,7 +89,7 @@ protected function buildClass($name): string
*/
protected function getPath($name): string
{
return resource_path(config('paragon.enums.paths.methods')) . "/{$this->name()}.ts";
return resource_path(config()->string('paragon.enums.paths.methods')) . "/{$this->name()}.ts";
}

/**
Expand Down
36 changes: 23 additions & 13 deletions src/Concerns/DiscoverEnums.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Kirschbaum\Paragon\Concerns;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionException;
use SplFileInfo;
Expand All @@ -14,9 +13,9 @@ class DiscoverEnums
/**
* Get all the enums by searching the given directory.
*
* @param array<int, string>|string $path
* @param array<int,string>|string $path
*
* @return Collection<class-string, class-string>
* @return Collection<class-string<\UnitEnum>,class-string<\UnitEnum>>
*/
public static function within(array|string $path): Collection
{
Expand All @@ -26,12 +25,17 @@ public static function within(array|string $path): Collection
/**
* Filter the files down to only enums.
*
* @return Collection<class-string, class-string>
* @param Finder<string,SplFileInfo> $files
*
* @return Collection<class-string<\UnitEnum>,class-string<\UnitEnum>>
*/
protected static function getEnums(Finder $files): Collection
{
return collect($files)
->mapWithKeys(function ($file) {
/** @var Collection<int, SplFileInfo> $fileCollection */
$fileCollection = collect($files);

return $fileCollection
->mapWithKeys(function (SplFileInfo $file) {
try {
if (! class_exists($enum = static::classFromFile($file))) {
return [];
Expand All @@ -51,15 +55,21 @@ protected static function getEnums(Finder $files): Collection

/**
* Extract the class name from the given file path.
*
* @return class-string<\UnitEnum>
*/
protected static function classFromFile(SplFileInfo $file): string
{
$class = trim(Str::replaceFirst(base_path(), '', $file->getRealPath()), DIRECTORY_SEPARATOR);

return str_replace(
[DIRECTORY_SEPARATOR, ucfirst(basename(app()->path())) . '\\'],
['\\', app()->getNamespace()],
ucfirst(Str::replaceLast('.php', '', $class))
);
/** @var class-string<\UnitEnum> */
return str($file->getRealPath())
->replaceFirst(base_path(), '')
->trim(DIRECTORY_SEPARATOR)
->replaceLast('.php', '')
->ucfirst()
->replace(
search: [DIRECTORY_SEPARATOR, ucfirst(basename(app()->path())) . '\\'],
replace: ['\\', app()->getNamespace()]
)
->toString();
}
}
20 changes: 13 additions & 7 deletions src/Generators/AbstractEnumGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use SplFileInfo;
use Symfony\Component\Filesystem\Filesystem as FileUtility;
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
use Symfony\Component\Finder\Finder;
Expand All @@ -16,7 +17,7 @@ class AbstractEnumGenerator
public function __construct()
{
$this->files = Storage::createLocalDriver([
'root' => resource_path(config('paragon.enums.paths.generated')),
'root' => resource_path(config()->string('paragon.enums.paths.generated')),
]);
}

Expand All @@ -34,7 +35,7 @@ protected function contents(): string
$suffix = $imports->count() ? PHP_EOL : '';

return str((string) file_get_contents($this->stubPath()))
->replace('{{ Abstract }}', config('paragon.enums.abstract-class'))
->replace('{{ Abstract }}', config()->string('paragon.enums.abstract-class'))
->replace('{{ Imports }}', "{$imports->join('')}{$suffix}")
->replace('{{ Methods }}', "{$this->methods($imports->keys())}{$suffix}");
}
Expand All @@ -57,22 +58,26 @@ protected function imports(): Collection
try {
$files = Finder::create()
->files()
->in(resource_path(config('paragon.enums.paths.methods')));
->in(resource_path(config()->string('paragon.enums.paths.methods')));
} catch (DirectoryNotFoundException) {
return collect();
}

return collect($files)
->mapWithKeys(function ($file) {
/** @var Collection<int, SplFileInfo> $fileCollection */
$fileCollection = collect($files);

return $fileCollection
->mapWithKeys(function (SplFileInfo $file): array {
$filesystem = new FileUtility();

$relativeFilePath = $filesystem->makePathRelative(
$file->getPath(),
resource_path(config('paragon.enums.paths.generated'))
resource_path(config()->string('paragon.enums.paths.generated'))
);

$name = (string) str($file->getFileName())->before('.');

/** @var array<string,string> */
return [$name => "import {$name} from '{$relativeFilePath}{$file->getFilename()}';" . PHP_EOL];
})
->sort();
Expand All @@ -85,7 +90,8 @@ protected function imports(): Collection
*/
protected function methods(Collection $methods): string
{
return $methods->map(fn ($method) => PHP_EOL . "Enum.{$method} = {$method};")
return $methods
->map(fn (string $method): string => PHP_EOL . "Enum.{$method} = {$method};")
->join('');
}

Expand Down
Loading

0 comments on commit 3897980

Please sign in to comment.