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

[feature] add static analysis tool #2664

Merged
merged 43 commits into from
Nov 23, 2023

Conversation

Treggats
Copy link
Contributor

@Treggats Treggats commented Nov 2, 2023

Introduce PHPStan as static analyser. Continues the work done on #2471

resolves #1930

This PR does the following

  1. add a basic config for PHPStan
  2. resolve issues found for level 1
  3. add a baseline for issues found in level 2
  4. add a job to the coding standard Github Workflow

.editorconfig Outdated Show resolved Hide resolved
@@ -2,8 +2,6 @@ name: CI

on:
push:
branches:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant, and not according to the Github Workflow schema


analysis:
runs-on: "ubuntu-22.04"
continue-on-error: true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will allow the job to continue even if PHPStan finds errors.

For now mostly a convenience to see the output of runs for both PHP 8.1 and 8.2

@@ -53,3 +51,29 @@ jobs:
# The -q option is required until phpcs v4 is released
- name: "Run PHP_CodeSniffer"
run: "vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr"

analysis:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Borrowed from the changes that @divine made in #2471

.github/workflows/coding-standards.yml Outdated Show resolved Hide resolved
composer.json Outdated Show resolved Hide resolved
@@ -0,0 +1,46 @@
parameters:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The baseline provides PHPStan with a list of errors to ignore, kind of saying

I know these issues exist, will fix it later

editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%'

ignoreErrors:
- '#Unsafe usage of new static#'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These issues were the remaining errors from level 1, I wasn't sure how to fix them. So I chose to ignore them.

@@ -1,13 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
backupGlobals="false"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed options that had the default value.

@@ -82,11 +82,11 @@ protected function setWhere()
}

/** @inheritdoc */
public function save(Model $model, array $joining = [], $touch = true)
public function save(Model $model, array $pivotAttributes = [], $touch = true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parent method argument name was changed, updated it to match it.


[*.yml]
indent_size = 2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Match the current indenting.

@Treggats Treggats marked this pull request as ready for review November 3, 2023 00:05
- ./phpstan-baseline.neon

parameters:
tmpDir: .cache/phpstan
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will speedup subsequent runs, in a Github Workflow we'd need to use the cache action to store this cache

composer.json Outdated Show resolved Hide resolved
src/Relations/EmbedsMany.php Show resolved Hide resolved
* @return int
*/
public function delete()
public function delete($id = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, can we remove this argument, since it's not used.

src/Relations/EmbedsOneOrMany.php Show resolved Hide resolved
* @return int
*/
public function count()
public function count($columns = '*')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used.

composer.json Outdated Show resolved Hide resolved
.github/workflows/coding-standards.yml Outdated Show resolved Hide resolved
@Treggats Treggats force-pushed the feature/1930-add-phpstan branch from b4989e5 to 848bed3 Compare November 3, 2023 23:42
$builder = $this->toBase();
assert($builder instanceof MongoDBQueryBuilder);

return $builder->update($this->addUpdatedAtColumn($values), $options);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is an interesting case. Both the IDE and PHPStan think that Illuminate\Database\Query\Builder is used, while actually it's MongoDB\Laravel\Query\Builder.
Adding the assertion is quickest/eastiest way to make sure the correct one is used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can overload the return type of toBase with an annotation on this class.

/** @method \MongoDB\Laravel\Query\Builder toBase() */
class Builder extends EloquentBuilder

@Treggats Treggats requested a review from GromNaN November 3, 2023 23:50
@Treggats Treggats force-pushed the feature/1930-add-phpstan branch 2 times, most recently from 9f5e849 to 06a405f Compare November 4, 2023 00:46
$this->query = $query;
$this->parent = $parent;
parent::__construct($query, $parent);

$this->related = $related;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property is also being set in the parent constructor, but when I tried to change it a few mocks broke and I'm not sure how to update those. So I left it alone 😅

@Treggats
Copy link
Contributor Author

Treggats commented Nov 4, 2023

Putting in draft while I figure out why the tests are faling

@Treggats Treggats marked this pull request as draft November 4, 2023 01:09
@Treggats Treggats force-pushed the feature/1930-add-phpstan branch 2 times, most recently from c48f18e to 6014fc6 Compare November 15, 2023 21:44
.cache/.gitignore Outdated Show resolved Hide resolved
.github/workflows/build-ci.yml Show resolved Hide resolved
@@ -3,9 +3,9 @@
*.sublime-workspace
.DS_Store
.idea/
.phpunit.cache/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I introduced the .cache directory for caching phpunit/phpcs/phpstan

@@ -20,10 +18,15 @@
<env name="MONGODB_DATABASE" value="unittest"/>
<env name="SQLITE_DATABASE" value=":memory:"/>
<env name="QUEUE_CONNECTION" value="database"/>

<ini name="memory_limit" value="-1"/>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While running the tests locally in Docker I ran into the issue that there wasn't enough memory. Disabling the limit here, seems like the most logical place

<include>
<directory suffix=".php">./src</directory>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the suffix was redundant so it could be removed

@Treggats Treggats marked this pull request as ready for review November 15, 2023 21:49
@Treggats Treggats requested review from GromNaN and divine November 16, 2023 08:53
.cache/.gitignore Outdated Show resolved Hide resolved
.github/workflows/build-ci.yml Outdated Show resolved Hide resolved
src/Eloquent/Builder.php Outdated Show resolved Hide resolved
src/Eloquent/Builder.php Outdated Show resolved Hide resolved
src/Relations/EmbedsMany.php Show resolved Hide resolved
@Treggats Treggats force-pushed the feature/1930-add-phpstan branch 2 times, most recently from 5f48433 to d26a9cd Compare November 16, 2023 11:41
src/Relations/EmbedsOneOrMany.php Outdated Show resolved Hide resolved
@@ -82,7 +86,7 @@ public function performUpdate(Model $model)
// Get the correct foreign key value.
$foreignKey = $this->getForeignKeyValue($model);

$values = $this->getUpdateValues($model->getDirty(), $this->localKey . '.$.');
$values = self::getUpdateValues($model->getDirty(), $this->localKey . '.$.');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method is static, so using a static call here

@Treggats Treggats requested a review from GromNaN November 16, 2023 11:46
@Treggats Treggats force-pushed the feature/1930-add-phpstan branch from d26a9cd to 31157c0 Compare November 16, 2023 12:11
@Treggats Treggats force-pushed the feature/1930-add-phpstan branch from 42cf4a3 to 48adaeb Compare November 22, 2023 15:40
Co-authored-by: Mohammad Mortazavi <39920372+hans-thomas@users.noreply.github.com>
phpstan-baseline.neon Outdated Show resolved Hide resolved
Treggats and others added 2 commits November 23, 2023 14:19
Co-authored-by: Mohammad Mortazavi <39920372+hans-thomas@users.noreply.github.com>
Co-authored-by: Mohammad Mortazavi <39920372+hans-thomas@users.noreply.github.com>
*
* @return array
*/
protected function formatSyncList(array $records)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing in favor of formatRecordList as mentioned in the comment

Co-authored-by: Mohammad Mortazavi <39920372+hans-thomas@users.noreply.github.com>
@Treggats Treggats requested a review from hans-thomas November 23, 2023 14:44
@GromNaN GromNaN merged commit 973b6b9 into mongodb:4.1 Nov 23, 2023
15 checks passed
@GromNaN
Copy link
Member

GromNaN commented Nov 23, 2023

Thank you @Treggats

@Treggats
Copy link
Contributor Author

Thank you @Treggats

You're welcome @GromNaN was a nice experience

@Treggats Treggats deleted the feature/1930-add-phpstan branch November 23, 2023 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use static analyzer
5 participants