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

chore: Setup PHPStan Level 5 #3553

Merged
merged 20 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ indent_size = 4

[tsconfig.json]
indent_size = 2

[*.neon]
indent_style = tab
36 changes: 36 additions & 0 deletions .github/workflows/REUSABLE_backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ on:
default: true
required: false

enable_phpstan:
description: "Enable PHPStan Static Analysis?"
type: boolean
default: false
required: false

backend_directory:
description: The directory of the project where backend code is located. This should contain a `composer.json` file, and is generally the root directory of the repo.
type: string
Expand Down Expand Up @@ -110,3 +116,33 @@ jobs:
working-directory: ${{ inputs.backend_directory }}
env:
COMPOSER_PROCESS_TIMEOUT: 600

phpstan:
runs-on: ubuntu-latest

strategy:
matrix:
php: ${{ fromJSON(inputs.php_versions) }}

name: 'PHPStan PHP ${{ matrix.php }}'

if: inputs.enable_phpstan

steps:
- uses: actions/checkout@master

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
extensions: curl, dom, gd, json, mbstring, openssl, pdo_mysql, tokenizer, zip
tools: phpunit, composer:v2
ini-values: ${{ inputs.php_ini_values }}

- name: Install Composer dependencies
run: composer install
working-directory: ${{ inputs.backend_directory }}

- name: Run PHPStan
run: composer analyse:phpstan
12 changes: 12 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Framework PHP

on: [workflow_dispatch, push, pull_request]

jobs:
run:
uses: ./.github/workflows/REUSABLE_backend.yml
with:
enable_backend_testing: false
enable_phpstan: true

backend_directory: .
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@
"require-dev": {
"mockery/mockery": "^1.4",
"phpunit/phpunit": "^9.0",
"phpstan/phpstan-php-parser": "^1.0",
"phpstan/phpstan": "^1.2"
"phpstan/phpstan": "^1.2",
"nunomaduro/larastan": "^1.0"
},
"config": {
"sort-packages": true
Expand Down Expand Up @@ -178,5 +178,11 @@
"extension.neon"
]
}
},
"scripts": {
"analyse:phpstan": "phpstan analyse"
},
"scripts-descriptions": {
"analyse:phpstan": "Run static analysis"
}
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 5 additions & 0 deletions framework/core/src/Admin/Content/AdminPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class AdminPayload
*/
protected $db;

/**
* @var Dispatcher
*/
protected $events;

/**
* @param Container $container
* @param SettingsRepositoryInterface $settings
Expand Down
8 changes: 2 additions & 6 deletions framework/core/src/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Flarum\Http\RequestUtil;
use Flarum\User\User;
use Illuminate\Contracts\Container\Container;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\Uri;
use Laminas\Stratigility\MiddlewarePipeInterface;
Expand All @@ -26,12 +25,12 @@ class Client
protected $pipe;

/**
* @var User
* @var User|null
*/
protected $actor;

/**
* @var ServerRequestInterface
* @var ServerRequestInterface|null
*/
protected $parent;

Expand All @@ -45,9 +44,6 @@ class Client
*/
protected $body = [];

/**
* @param Container $container
*/
public function __construct(MiddlewarePipeInterface $pipe)
{
$this->pipe = $pipe;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
protected static $beforeSerializationCallbacks = [];

/**
* @var string[]
* @var string[][]
*/
protected static $loadRelations = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ private function loadSubjectDiscussions(array $notifications)
$ids = [];

foreach ($notifications as $notification) {
if ($notification->subject && $notification->subject->discussion_id) {
if ($notification->subject && isset($notification->subject->discussion_id)) {
$ids[] = $notification->subject->discussion_id;
}
}

$discussions = Discussion::find(array_unique($ids));
$discussions = Discussion::query()->find(array_unique($ids));

foreach ($notifications as $notification) {
if ($notification->subject && $notification->subject->discussion_id) {
if ($notification->subject && isset($notification->subject->discussion_id)) {
$notification->subject->setRelation('discussion', $discussions->find($notification->subject->discussion_id));
}
}
Expand Down
10 changes: 6 additions & 4 deletions framework/core/src/Api/Serializer/AbstractSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ abstract class AbstractSerializer extends BaseAbstractSerializer
protected static $container;

/**
* @var callable[]
* @var array<string, callable[]>
*/
protected static $attributeMutators = [];

/**
* @var array
* @var array<string, array<string, callable>>
*/
protected static $customRelations = [];

Expand Down Expand Up @@ -189,12 +189,12 @@ public function hasMany($model, $serializer, $relation = null)
* @param string|Closure|\Tobscure\JsonApi\SerializerInterface $serializer
* @param string|null $relation
* @param bool $many
* @return Relationship
* @return Relationship|null
*/
protected function buildRelationship($model, $serializer, $relation = null, $many = false)
{
if (is_null($relation)) {
list(, , $caller) = debug_backtrace(false, 3);
list(, , $caller) = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 3);

$relation = $caller['function'];
}
Expand All @@ -210,6 +210,8 @@ protected function buildRelationship($model, $serializer, $relation = null, $man

return new Relationship($element);
}

return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
class ExtensionReadmeSerializer extends AbstractSerializer
{
/**
* {@inheritdoc}
* @param \Flarum\Extension\Extension $extension
* @return array
*/
protected function getDefaultAttributes($extension)
{
Expand Down
10 changes: 7 additions & 3 deletions framework/core/src/Api/Serializer/ForumSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Cloud;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Contracts\Filesystem\Filesystem;

class ForumSerializer extends AbstractSerializer
{
Expand All @@ -39,7 +40,7 @@ class ForumSerializer extends AbstractSerializer
protected $url;

/**
* @var Cloud
* @var Filesystem
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
*/
protected $assetsFilesystem;

Expand All @@ -62,7 +63,7 @@ public function __construct(Config $config, Factory $filesystemFactory, Settings
*/
public function getId($model)
{
return 1;
return '1';
}

/**
Expand Down Expand Up @@ -132,6 +133,9 @@ protected function getFaviconUrl()

public function getAssetUrl($assetPath): string
{
return $this->assetsFilesystem->url($assetPath);
/** @var Cloud $assetsFilesystem */
$assetsFilesystem = $this->assetsFilesystem;
SychO9 marked this conversation as resolved.
Show resolved Hide resolved

return $assetsFilesystem->url($assetPath);
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
}
}
7 changes: 5 additions & 2 deletions framework/core/src/Console/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ class Schedule extends LaravelSchedule
public function dueEvents($container)
{
return (new Collection($this->events))->filter->isDue(new class($container) {
/** @var Config */
protected $config;

public function __construct($container)
{
$this->config = $container->make(Config::class);
}

public function isDownForMaintenance()
public function isDownForMaintenance(): bool
{
return $this->config->inMaintenanceMode();
}

public function environment()
public function environment(): string
{
return '';
}
Expand Down
4 changes: 3 additions & 1 deletion framework/core/src/Database/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* Adds the ability for custom relations to be added to a model during runtime.
* These relations behave in the same way that you would expect; they can be
* queried, eager loaded, and accessed as an attribute.
*
* @property-read int|null $id
*/
abstract class AbstractModel extends Eloquent
{
Expand Down Expand Up @@ -82,7 +84,7 @@ public static function boot()
/**
* {@inheritdoc}
*/
public function __construct(array $attributes = [])
final public function __construct(array $attributes = [])
{
$this->attributes = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Flarum\Console\AbstractCommand;
use Flarum\Foundation\Paths;
use Illuminate\Database\Connection;
use Illuminate\Database\MySqlConnection;

class GenerateDumpCommand extends AbstractCommand
{
Expand Down Expand Up @@ -53,7 +54,7 @@ protected function configure()
protected function fire()
{
$dumpPath = __DIR__.'/../../../migrations/install.dump';
/** @var Connection */
/** @var Connection&MySqlConnection */
$connection = resolve('db.connection');

$connection
Expand Down
10 changes: 5 additions & 5 deletions framework/core/src/Database/DatabaseMigrationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

namespace Flarum\Database;

use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Connection;

class DatabaseMigrationRepository implements MigrationRepositoryInterface
{
/**
* The name of the database connection to use.
*
* @var ConnectionInterface
* @var Connection
*/
protected $connection;

Expand All @@ -30,10 +30,10 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
/**
* Create a new database migration repository instance.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param string $table
* @param Connection $connection
* @param string $table
*/
public function __construct(ConnectionInterface $connection, $table)
public function __construct(Connection $connection, $table)
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
{
$this->connection = $connection;
$this->table = $table;
Expand Down
1 change: 1 addition & 0 deletions framework/core/src/Database/DatabaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class DatabaseServiceProvider extends AbstractServiceProvider
public function register()
{
$this->container->singleton(Manager::class, function (Container $container) {
/** @var \Illuminate\Container\Container $container */
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
$manager = new Manager($container);

$config = $container['flarum']->config('database');
Expand Down
Loading