diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 14c7536..6e87ef8 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,17 +1,16 @@ -name: PHPCS check +name: PHP CS check -on: - push: - pull_request: - schedule: - - cron: '0 0 * * *' +on: [push] jobs: phpcs: - name: PHPCS + name: PHP CS runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - name: Checkout code + uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -21,7 +20,5 @@ jobs: - name: Install dependencies run: export COMPOSER_ROOT_VERSION=dev-master && composer install --prefer-dist --no-progress --no-suggest - - name: PHPCS check - uses: chekalsky/phpcs-action@v1 - with: - phpcs_bin_path: './vendor/bin/phpcs' + - name: PHP CS check + run: 'composer lint' diff --git a/phpcs.xml b/phpcs.xml index 8641ed6..64e31b9 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,7 +1,7 @@ - + diff --git a/src/Fakes/FakeModelsCommand.php b/src/Fakes/FakeModelsCommand.php index 6dcc731..1350608 100644 --- a/src/Fakes/FakeModelsCommand.php +++ b/src/Fakes/FakeModelsCommand.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Str; use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator; + use function config; use function get_class; use function implode; @@ -37,7 +38,7 @@ class FakeModelsCommand extends ModelsCommand * * @param Model $model */ - protected function getPropertiesFromTable($model) : void + protected function getPropertiesFromTable($model): void { $table_name = $model->getTable(); diff --git a/src/Handlers/Application/ContainerHandler.php b/src/Handlers/Application/ContainerHandler.php index b91382e..6142779 100644 --- a/src/Handlers/Application/ContainerHandler.php +++ b/src/Handlers/Application/ContainerHandler.php @@ -17,6 +17,7 @@ use Psalm\Type\Atomic\TNamedObject; use Psalm\Type\Union; use ReflectionClass; use Throwable; + use function array_filter; use function array_keys; use function get_class; @@ -54,7 +55,7 @@ final class ContainerHandler implements AfterClassLikeVisitInterface, FunctionRe return [get_class(ApplicationProvider::getApp())]; } - public static function getMethodReturnType(MethodReturnTypeProviderEvent $event) : ?Type\Union + public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Type\Union { // lumen doesn't have the likes of makeWith, so we will ensure these methods actually exist on the underlying // app contract diff --git a/src/Handlers/Application/OffsetHandler.php b/src/Handlers/Application/OffsetHandler.php index f4fbb33..3902a92 100644 --- a/src/Handlers/Application/OffsetHandler.php +++ b/src/Handlers/Application/OffsetHandler.php @@ -14,6 +14,7 @@ use Psalm\Plugin\EventHandler\MethodParamsProviderInterface; use Psalm\Plugin\EventHandler\MethodReturnTypeProviderInterface; use Psalm\Plugin\EventHandler\MethodVisibilityProviderInterface; use Psalm\Type; + use function in_array; final class OffsetHandler implements diff --git a/src/Handlers/Eloquent/ModelMethodHandler.php b/src/Handlers/Eloquent/ModelMethodHandler.php index 9ca4104..aa9629a 100644 --- a/src/Handlers/Eloquent/ModelMethodHandler.php +++ b/src/Handlers/Eloquent/ModelMethodHandler.php @@ -1,4 +1,6 @@ -getSource(); @@ -73,7 +76,8 @@ final class ModelMethodHandler implements MethodReturnTypeProviderInterface, Aft public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event) { $storage = $event->getStorage(); - if ($event->getStmt() instanceof Class_ + if ( + $event->getStmt() instanceof Class_ && !$storage->abstract && isset($storage->parent_classes[strtolower(Model::class)]) ) { diff --git a/src/Handlers/Eloquent/ModelPropertyAccessorHandler.php b/src/Handlers/Eloquent/ModelPropertyAccessorHandler.php index 71c9814..33fb48b 100644 --- a/src/Handlers/Eloquent/ModelPropertyAccessorHandler.php +++ b/src/Handlers/Eloquent/ModelPropertyAccessorHandler.php @@ -11,6 +11,7 @@ use Psalm\Plugin\EventHandler\PropertyExistenceProviderInterface; use Psalm\Plugin\EventHandler\PropertyTypeProviderInterface; use Psalm\Plugin\EventHandler\PropertyVisibilityProviderInterface; use Psalm\Type; + use function str_replace; final class ModelPropertyAccessorHandler implements PropertyExistenceProviderInterface, PropertyVisibilityProviderInterface, PropertyTypeProviderInterface diff --git a/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php b/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php index 229078c..6ab89b1 100644 --- a/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php +++ b/src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php @@ -20,6 +20,7 @@ use Psalm\Plugin\EventHandler\PropertyVisibilityProviderInterface; use Psalm\Type; use Psalm\Type\Atomic\TGenericObject; use Psalm\Type\Union; + use function in_array; class ModelRelationshipPropertyHandler implements @@ -28,12 +29,12 @@ class ModelRelationshipPropertyHandler implements PropertyTypeProviderInterface { /** @return array */ - public static function getClassLikeNames() : array + public static function getClassLikeNames(): array { return ModelStubProvider::getModelClasses(); } - public static function doesPropertyExist(PropertyExistenceProviderEvent $event) : ?bool + public static function doesPropertyExist(PropertyExistenceProviderEvent $event): ?bool { $source = $event->getSource(); @@ -58,7 +59,7 @@ class ModelRelationshipPropertyHandler implements return null; } - public static function isPropertyVisible(PropertyVisibilityProviderEvent $event) : ?bool + public static function isPropertyVisible(PropertyVisibilityProviderEvent $event): ?bool { if (!$event->isReadMode()) { return null; @@ -86,7 +87,7 @@ class ModelRelationshipPropertyHandler implements * * @return ?Union */ - public static function getPropertyType(PropertyTypeProviderEvent $event) : ?Union + public static function getPropertyType(PropertyTypeProviderEvent $event): ?Union { $source = $event->getSource(); diff --git a/src/Handlers/Eloquent/RelationsMethodHandler.php b/src/Handlers/Eloquent/RelationsMethodHandler.php index 32c7cbd..7ea0b6c 100644 --- a/src/Handlers/Eloquent/RelationsMethodHandler.php +++ b/src/Handlers/Eloquent/RelationsMethodHandler.php @@ -1,4 +1,6 @@ -getSource(); @@ -57,7 +59,8 @@ final class RelationsMethodHandler implements MethodReturnTypeProviderInterface // If this method name is on the builder object, proxy it over there - if ($source->getCodebase()->methods->methodExists(new MethodIdentifier(Builder::class, $method_name_lowercase)) || + if ( + $source->getCodebase()->methods->methodExists(new MethodIdentifier(Builder::class, $method_name_lowercase)) || $source->getCodebase()->methods->methodExists(new MethodIdentifier(QueryBuilder::class, $method_name_lowercase)) ) { $template_type_parameters = $event->getTemplateTypeParameters(); diff --git a/src/Handlers/Eloquent/Schema/SchemaAggregator.php b/src/Handlers/Eloquent/Schema/SchemaAggregator.php index 1743669..5899656 100644 --- a/src/Handlers/Eloquent/Schema/SchemaAggregator.php +++ b/src/Handlers/Eloquent/Schema/SchemaAggregator.php @@ -5,6 +5,7 @@ namespace Psalm\LaravelPlugin\Handlers\Eloquent\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; use PhpParser; + use function count; use function is_string; use function strtolower; @@ -17,7 +18,7 @@ class SchemaAggregator /** * @param array $stmts */ - public function addStatements(array $stmts) : void + public function addStatements(array $stmts): void { foreach ($stmts as $stmt) { if ($stmt instanceof PhpParser\Node\Stmt\Class_) { @@ -29,10 +30,11 @@ class SchemaAggregator /** * @param array $stmts */ - private function addClassStatements(array $stmts) : void + private function addClassStatements(array $stmts): void { foreach ($stmts as $stmt) { - if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod + if ( + $stmt instanceof PhpParser\Node\Stmt\ClassMethod && $stmt->name->name === 'up' && $stmt->stmts ) { @@ -44,10 +46,11 @@ class SchemaAggregator /** * @param array $stmts */ - private function addUpMethodStatements(array $stmts) : void + private function addUpMethodStatements(array $stmts): void { foreach ($stmts as $stmt) { - if ($stmt instanceof PhpParser\Node\Stmt\Expression + if ( + $stmt instanceof PhpParser\Node\Stmt\Expression && $stmt->expr instanceof PhpParser\Node\Expr\StaticCall && $stmt->expr->class instanceof PhpParser\Node\Name && $stmt->expr->name instanceof PhpParser\Node\Identifier @@ -74,9 +77,10 @@ class SchemaAggregator } } - private function alterTable(PhpParser\Node\Expr\StaticCall $call, bool $creating) : void + private function alterTable(PhpParser\Node\Expr\StaticCall $call, bool $creating): void { - if (!isset($call->args[0]) + if ( + !isset($call->args[0]) || !$call->args[0] instanceof PhpParser\Node\Arg || !$call->args[0]->value instanceof PhpParser\Node\Scalar\String_ ) { @@ -89,7 +93,8 @@ class SchemaAggregator $this->tables[$table_name] = new SchemaTable($table_name); } - if (!isset($call->args[1]) + if ( + !isset($call->args[1]) || !$call->args[1] instanceof PhpParser\Node\Arg || !$call->args[1]->value instanceof PhpParser\Node\Expr\Closure || count($call->args[1]->value->params) < 1 @@ -102,7 +107,8 @@ class SchemaAggregator $update_closure = $call->args[1]->value; - if ($call->args[1]->value->params[0]->var instanceof PhpParser\Node\Expr\Variable + if ( + $call->args[1]->value->params[0]->var instanceof PhpParser\Node\Expr\Variable && is_string($call->args[1]->value->params[0]->var->name) ) { $call_arg_name = $call->args[1]->value->params[0]->var->name; @@ -111,9 +117,10 @@ class SchemaAggregator } } - private function dropTable(PhpParser\Node\Expr\StaticCall $call) : void + private function dropTable(PhpParser\Node\Expr\StaticCall $call): void { - if (!isset($call->args[0]) + if ( + !isset($call->args[0]) || !$call->args[0] instanceof PhpParser\Node\Arg || !$call->args[0]->value instanceof PhpParser\Node\Scalar\String_ ) { @@ -125,9 +132,10 @@ class SchemaAggregator unset($this->tables[$table_name]); } - private function renameTable(PhpParser\Node\Expr\StaticCall $call) : void + private function renameTable(PhpParser\Node\Expr\StaticCall $call): void { - if (!isset($call->args[0], $call->args[1]) + if ( + !isset($call->args[0], $call->args[1]) || !$call->args[0] instanceof PhpParser\Node\Arg || !$call->args[0]->value instanceof PhpParser\Node\Scalar\String_ || !$call->args[1] instanceof PhpParser\Node\Arg @@ -152,7 +160,7 @@ class SchemaAggregator $this->tables[$new_table_name] = $table; } - private function processColumnUpdates(string $table_name, string $call_arg_name, array $stmts) : void + private function processColumnUpdates(string $table_name, string $call_arg_name, array $stmts): void { if (!isset($this->tables[$table_name])) { return; @@ -161,7 +169,8 @@ class SchemaAggregator $table = $this->tables[$table_name]; foreach ($stmts as $stmt) { - if ($stmt instanceof PhpParser\Node\Stmt\Expression + if ( + $stmt instanceof PhpParser\Node\Stmt\Expression && $stmt->expr instanceof PhpParser\Node\Expr\MethodCall && $stmt->expr->name instanceof PhpParser\Node\Identifier ) { @@ -172,7 +181,8 @@ class SchemaAggregator $nullable = false; while ($root_var instanceof PhpParser\Node\Expr\MethodCall) { - if ($root_var->name instanceof PhpParser\Node\Identifier + if ( + $root_var->name instanceof PhpParser\Node\Identifier && $root_var->name->name === 'nullable' ) { $nullable = true; @@ -182,7 +192,8 @@ class SchemaAggregator $root_var = $root_var->var; } - if ($root_var instanceof PhpParser\Node\Expr\Variable + if ( + $root_var instanceof PhpParser\Node\Expr\Variable && $root_var->name === $call_arg_name && $first_method_call->name instanceof PhpParser\Node\Identifier ) { @@ -190,7 +201,8 @@ class SchemaAggregator $second_arg = $first_method_call->args[1]->value ?? null; if (!$first_arg instanceof PhpParser\Node\Scalar\String_) { - if ($first_method_call->name->name === 'timestamps' + if ( + $first_method_call->name->name === 'timestamps' || $first_method_call->name->name === 'timestampsTz' || $first_method_call->name->name === 'nullableTimestamps' || $first_method_call->name->name === 'nullableTimestampsTz' @@ -228,7 +240,8 @@ class SchemaAggregator } continue; - } elseif ($first_method_call->name->name === 'softDeletes' + } elseif ( + $first_method_call->name->name === 'softDeletes' || $first_method_call->name->name === 'softDeletesTz' || $first_method_call->name->name === 'dropSoftDeletes' || $first_method_call->name->name === 'dropSoftDeletesTz' diff --git a/src/Handlers/Eloquent/Schema/SchemaTable.php b/src/Handlers/Eloquent/Schema/SchemaTable.php index 5bc4d00..fce73dd 100644 --- a/src/Handlers/Eloquent/Schema/SchemaTable.php +++ b/src/Handlers/Eloquent/Schema/SchemaTable.php @@ -15,12 +15,12 @@ class SchemaTable $this->name = $name; } - public function setColumn(SchemaColumn $column) : void + public function setColumn(SchemaColumn $column): void { $this->columns[$column->name] = $column; } - public function renameColumn(string $old_name, string $new_name) : void + public function renameColumn(string $old_name, string $new_name): void { if (!isset($this->columns[$old_name])) { return; @@ -35,7 +35,7 @@ class SchemaTable $this->columns[$new_name] = $old_column; } - public function dropColumn(string $column_name) : void + public function dropColumn(string $column_name): void { unset($this->columns[$column_name]); } diff --git a/src/Handlers/Helpers/PathHandler.php b/src/Handlers/Helpers/PathHandler.php index 6b5d6f3..ac6ffb1 100644 --- a/src/Handlers/Helpers/PathHandler.php +++ b/src/Handlers/Helpers/PathHandler.php @@ -1,4 +1,6 @@ -getFunctionId(); @@ -40,7 +43,7 @@ final class PathHandler implements FunctionReturnTypeProviderInterface, MethodRe ]; } - public static function getMethodReturnType(MethodReturnTypeProviderEvent $event) : ?Union + public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Union { $methods = ['path', 'basepath', 'configpath', 'databasepath', 'resourcepath']; diff --git a/src/Handlers/Helpers/RedirectHandler.php b/src/Handlers/Helpers/RedirectHandler.php index 7aa5f7f..e35b645 100644 --- a/src/Handlers/Helpers/RedirectHandler.php +++ b/src/Handlers/Helpers/RedirectHandler.php @@ -1,4 +1,6 @@ - */ @@ -25,7 +26,7 @@ class RedirectHandler implements FunctionReturnTypeProviderInterface * * @return ?Type\Union */ - public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event) : ?Type\Union + public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Type\Union { if (!$event->getCallArgs()) { return new Type\Union([ diff --git a/src/Handlers/Helpers/TransHandler.php b/src/Handlers/Helpers/TransHandler.php index 2d6b34f..0c5f61c 100644 --- a/src/Handlers/Helpers/TransHandler.php +++ b/src/Handlers/Helpers/TransHandler.php @@ -8,12 +8,12 @@ use Psalm\Type; class TransHandler implements FunctionReturnTypeProviderInterface { - public static function getFunctionIds() : array + public static function getFunctionIds(): array { return ['trans']; } - public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event) : Type\Union + public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): Type\Union { $call_args = $event->getCallArgs(); diff --git a/src/Handlers/Helpers/UrlHandler.php b/src/Handlers/Helpers/UrlHandler.php index 9427786..530be40 100644 --- a/src/Handlers/Helpers/UrlHandler.php +++ b/src/Handlers/Helpers/UrlHandler.php @@ -1,4 +1,6 @@ -getCallArgs()) { return new Union([ diff --git a/src/Handlers/Helpers/ViewHandler.php b/src/Handlers/Helpers/ViewHandler.php index a5ab153..933ad9d 100644 --- a/src/Handlers/Helpers/ViewHandler.php +++ b/src/Handlers/Helpers/ViewHandler.php @@ -11,12 +11,12 @@ use Psalm\Type\Atomic\TNamedObject; class ViewHandler implements FunctionReturnTypeProviderInterface { - public static function getFunctionIds() : array + public static function getFunctionIds(): array { return ['view']; } - public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event) : Type\Union + public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): Type\Union { if ($event->getCallArgs()) { return new Type\Union([ diff --git a/src/Handlers/SuppressHandler.php b/src/Handlers/SuppressHandler.php index f15895e..98f72e6 100644 --- a/src/Handlers/SuppressHandler.php +++ b/src/Handlers/SuppressHandler.php @@ -7,6 +7,7 @@ use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent; use Psalm\Storage\ClassLikeStorage; use Psalm\Storage\MethodStorage; use Psalm\Storage\PropertyStorage; + use function array_intersect; use function in_array; use function strpos; diff --git a/src/Plugin.php b/src/Plugin.php index 29fa34c..c28bc1b 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -1,4 +1,5 @@ createApplication(); + $app = (new self())->createApplication(); } self::$app = $app; diff --git a/src/Providers/FacadeStubProvider.php b/src/Providers/FacadeStubProvider.php index 1e37003..2a0cce4 100644 --- a/src/Providers/FacadeStubProvider.php +++ b/src/Providers/FacadeStubProvider.php @@ -7,6 +7,7 @@ use Illuminate\Config\Repository; use Psalm\LaravelPlugin\Fakes\FakeFilesystem; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\NullOutput; + use function unlink; final class FacadeStubProvider implements GeneratesStubs diff --git a/src/Providers/ModelStubProvider.php b/src/Providers/ModelStubProvider.php index 62a2a25..6375aa3 100644 --- a/src/Providers/ModelStubProvider.php +++ b/src/Providers/ModelStubProvider.php @@ -8,13 +8,13 @@ use Psalm\LaravelPlugin\Fakes\FakeModelsCommand; use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\NullOutput; + use function dirname; use function glob; use function unlink; final class ModelStubProvider implements GeneratesStubs { - /** * @var array */ diff --git a/src/Providers/ViewFactoryProvider.php b/src/Providers/ViewFactoryProvider.php index 2768219..8f45374 100644 --- a/src/Providers/ViewFactoryProvider.php +++ b/src/Providers/ViewFactoryProvider.php @@ -11,6 +11,7 @@ use Illuminate\View\FileViewFinder; use Psalm\LaravelPlugin\Fakes\FakeFilesystem; use ReflectionClass; use UnexpectedValueException; + use function dirname; final class ViewFactoryProvider @@ -27,7 +28,7 @@ final class ViewFactoryProvider $resolver = new EngineResolver(); $fake_filesystem = new FakeFilesystem(); - $resolver->register('php', function () use ($fake_filesystem) : PhpEngine { + $resolver->register('php', function () use ($fake_filesystem): PhpEngine { return new PhpEngine($fake_filesystem); }); $finder = new FileViewFinder($fake_filesystem, [dirname($file_path) . '/../resources/views']); diff --git a/src/Util/ContainerResolver.php b/src/Util/ContainerResolver.php index 53ea41a..16c8577 100644 --- a/src/Util/ContainerResolver.php +++ b/src/Util/ContainerResolver.php @@ -1,4 +1,6 @@ -node_data; $statements_analyzer->node_data = clone $statements_analyzer->node_data; @@ -40,12 +41,14 @@ final class ProxyMethodReturnTypeProvider $statements_analyzer->addSuppressedIssues(['PossiblyInvalidMethodCall']); } - if (MethodCallAnalyzer::analyze( - $statements_analyzer, - $fake_method_call, - $context, - false - ) === false) { + if ( + MethodCallAnalyzer::analyze( + $statements_analyzer, + $fake_method_call, + $context, + false + ) === false + ) { return null; }