use PSR12 instead of PSR2

PSR2 is deprecated in favor of PSR12
This commit is contained in:
Niels Vanpachtenbeke 2022-01-19 10:01:32 +01:00
parent ee38778df7
commit 2ccac3e3e2
24 changed files with 112 additions and 67 deletions

View File

@ -3,15 +3,16 @@ name: PHPCS check
on:
push:
pull_request:
schedule:
- cron: '0 0 * * *'
jobs:
phpcs:
name: PHPCS
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

View File

@ -1,7 +1,7 @@
<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
<config name="installed_paths" value="../../slevomat/coding-standard"/>
<rule ref="PSR2" />
<rule ref="PSR12" />
<!-- Forbid usage of a function or a class constant via fallback global name -->
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
<properties>

View File

@ -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();

View File

@ -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

View File

@ -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

View File

@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Psalm\LaravelPlugin\Handlers\Eloquent;
@ -17,6 +19,7 @@ use Psalm\Plugin\EventHandler\Event\MethodReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\MethodReturnTypeProviderInterface;
use Psalm\Type;
use Psalm\Type\Union;
use function strtolower;
final class ModelMethodHandler implements MethodReturnTypeProviderInterface, AfterClassLikeVisitInterface
@ -29,7 +32,7 @@ final class ModelMethodHandler implements MethodReturnTypeProviderInterface, Aft
return [Model::class];
}
public static function getMethodReturnType(MethodReturnTypeProviderEvent $event) : ?Type\Union
public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Type\Union
{
$source = $event->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)])
) {

View File

@ -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

View File

@ -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<string, string> */
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();

View File

@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Psalm\LaravelPlugin\Handlers\Eloquent;
@ -41,7 +43,7 @@ final class RelationsMethodHandler implements MethodReturnTypeProviderInterface
];
}
public static function getMethodReturnType(MethodReturnTypeProviderEvent $event) : ?Union
public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Union
{
$source = $event->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();

View File

@ -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<int, PhpParser\Node\Stmt> $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<int, PhpParser\Node\Stmt> $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<int, PhpParser\Node\Stmt> $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'

View File

@ -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]);
}

View File

@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Psalm\LaravelPlugin\Handlers\Helpers;
@ -10,6 +12,7 @@ use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
use Psalm\Plugin\EventHandler\MethodReturnTypeProviderInterface;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Union;
use function get_class;
use function in_array;
use function is_string;
@ -21,7 +24,7 @@ final class PathHandler implements FunctionReturnTypeProviderInterface, MethodRe
return ['app_path', 'base_path', 'config_path', 'database_path', 'resource_path', 'public_path', 'storage_path'];
}
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event) : ?Union
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Union
{
$function_id = $event->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'];

View File

@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Psalm\LaravelPlugin\Handlers\Helpers;
@ -11,7 +13,6 @@ use Psalm\Type;
class RedirectHandler implements FunctionReturnTypeProviderInterface
{
/**
* @return array<lowercase-string>
*/
@ -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([

View File

@ -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();

View File

@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Psalm\LaravelPlugin\Handlers\Helpers;
@ -16,7 +18,7 @@ final class UrlHandler implements FunctionReturnTypeProviderInterface
return ['url'];
}
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event) : ?Union
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Union
{
if (!$event->getCallArgs()) {
return new Union([

View File

@ -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([

View File

@ -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;

View File

@ -1,4 +1,5 @@
<?php
namespace Psalm\LaravelPlugin;
use Illuminate\Foundation\Application;
@ -21,6 +22,7 @@ use Psalm\Plugin\PluginEntryPointInterface;
use Psalm\Plugin\RegistrationInterface;
use SimpleXMLElement;
use Throwable;
use function array_merge;
use function dirname;
use function explode;
@ -28,8 +30,7 @@ use function glob;
class Plugin implements PluginEntryPointInterface
{
public function __invoke(RegistrationInterface $registration, ?SimpleXMLElement $config = null) : void
public function __invoke(RegistrationInterface $registration, ?SimpleXMLElement $config = null): void
{
try {
ApplicationProvider::bootApp();
@ -51,7 +52,7 @@ class Plugin implements PluginEntryPointInterface
{
[$majorVersion] = explode('.', $version);
return glob(dirname(__DIR__) . '/stubs/'.$majorVersion.'/*.stubphp');
return glob(dirname(__DIR__) . '/stubs/' . $majorVersion . '/*.stubphp');
}
private function registerStubs(RegistrationInterface $registration): void

View File

@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Psalm\LaravelPlugin\Providers;
@ -8,6 +10,7 @@ use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Application as LaravelApplication;
use Laravel\Lumen\Application as LumenApplication;
use Orchestra\Testbench\Concerns\CreatesApplication;
use function file_exists;
use function get_class;
use function getcwd;
@ -48,12 +51,12 @@ final class ApplicationProvider
return self::$app;
}
if (file_exists($applicationPath = __DIR__.'/../../../../bootstrap/app.php')) { // Applications
if (file_exists($applicationPath = __DIR__ . '/../../../../bootstrap/app.php')) { // Applications
$app = require $applicationPath;
} elseif (file_exists($applicationPath = getcwd().'/bootstrap/app.php')) { // Local Dev
} elseif (file_exists($applicationPath = getcwd() . '/bootstrap/app.php')) { // Local Dev
$app = require $applicationPath;
} else { // Packages
$app = (new self)->createApplication();
$app = (new self())->createApplication();
}
self::$app = $app;

View File

@ -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

View File

@ -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<class-string>
*/

View File

@ -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']);

View File

@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Psalm\LaravelPlugin\Util;
@ -10,6 +12,7 @@ use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Union;
use ReflectionException;
use function array_key_exists;
use function class_exists;
use function count;

View File

@ -8,6 +8,7 @@ use Psalm\Internal\Analyzer\Statements\Expression\Call\MethodCallAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Union;
use function in_array;
final class ProxyMethodReturnTypeProvider
@ -23,7 +24,7 @@ final class ProxyMethodReturnTypeProvider
MethodCall $fake_method_call,
Context $context,
TNamedObject $typeToCall
) : ?Union {
): ?Union {
$old_data_provider = $statements_analyzer->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;
}