Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,30 @@ includes:

<br>

Do you use mocks in your PHPUnit tests? Enable mocking rules with single parameter:

```yaml
parameters:
mocks: true
```

<br>
### Symfony/Laravel container `->get()`/`->make()` return type extensions

Want sharper type inference? The return type extensions are **disabled by default** — enable the ones that fit your stack:

```yaml
parameters:
symfonyReturnType: true
laravelReturnType: true
pathStrings: true
symplify:
symfonyReturnType: true
laravelReturnType: true
pathStrings: true
```

`symfonyReturnType` resolves `$container->get(SomeService::class)` to `SomeService` and Symfony Finder's `$splFileInfo->getRealPath()` to `string`. `laravelReturnType` does the same for Laravel's `$container->make(SomeService::class)`. `pathStrings` narrows `getcwd()`, `dirname()` and `realpath()` to `string`:
* `symfonyReturnType` resolves
* `$container->get(SomeService::class)` to `SomeService` and
* Symfony Finder's `$splFileInfo->getRealPath()` to `string`

```php
$service = $container->get(SomeService::class);
// $service is now known as SomeService, instead of plain object
```

* `laravelReturnType` does the same for Laravel's `$container->make(SomeService::class)`
* `pathStrings` narrows `getcwd()`, `dirname()` and `realpath()` to `string`:

<br>

But at start, make baby steps with one rule at a time:
Expand All @@ -90,7 +89,8 @@ Tired of ever growing ignored error count in your `phpstan.neon`? Set hard limit

```yaml
parameters:
maximumIgnoredErrorCount: 50
symplify:
maximumIgnoredErrorCount: 50
```

<br>
Expand Down Expand Up @@ -123,7 +123,8 @@ This rule is disabled by default. Enable it with the `ctor` parameter:

```yaml
parameters:
ctor: true
symplify:
ctor: true
```

<br>
Expand Down Expand Up @@ -2706,7 +2707,8 @@ This set is for you! Enable all mocking rules with single parameter in your `php

```yaml
parameters:
mocks: true
symplify:
mocks: true
```

<br>
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"license": "MIT",
"require": {
"php": "^8.4",
"webmozart/assert": "^2.0",
"webmozart/assert": "^2.4",
"phpstan/phpstan": "^2.2",
"nette/utils": "^4.1",
"phpstan/phpdoc-parser": "^2.3"
},
"require-dev": {
"nikic/php-parser": "^5.7",
"phpunit/phpunit": "^13.0",
"symfony/framework-bundle": "^6.2",
"illuminate/container": "^11.0",
"symplify/easy-coding-standard": "^13.1",
"phpunit/phpunit": "^13.2",
"symfony/framework-bundle": "^6.4",
"illuminate/container": "^11.51",
"symplify/easy-coding-standard": "^13.2",
"tomasvotruba/class-leak": "^2.1",
"rector/rector": "^2.4",
"phpstan/extension-installer": "^1.4",
Expand Down
11 changes: 2 additions & 9 deletions config/ctor-rules.neon
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
parameters:
# enables NewOverSettersRule, disabled by default
ctor: false

parametersSchema:
ctor: bool()

services:
-
class: Symplify\PHPStanRules\Collector\NewWithFollowingSettersCollector
tags:
- phpstan.collector
arguments:
isEnabled: %ctor%
isEnabled: %symplify.ctor%

-
class: Symplify\PHPStanRules\Rules\NewOverSettersRule
tags:
- phpstan.rules.rule
arguments:
isEnabled: %ctor%
isEnabled: %symplify.ctor%
29 changes: 9 additions & 20 deletions config/mock-rules.neon
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
# PHPUnit mocking rules, enable in your phpstan.neon with:
#
# parameters:
# mocks: true

parameters:
mocks: false

parametersSchema:
mocks: bool()

conditionalTags:
# mocking
Symplify\PHPStanRules\Rules\PHPUnit\NoMockOnlyTestRule:
phpstan.rules.rule: %mocks%
phpstan.rules.rule: %symplify.mocks%
Symplify\PHPStanRules\Rules\PHPUnit\NoMockObjectAndRealObjectPropertyRule:
phpstan.rules.rule: %mocks%
phpstan.rules.rule: %symplify.mocks%

# overly complicated
Symplify\PHPStanRules\Rules\PHPUnit\NoDoubleConsecutiveTestMockRule:
phpstan.rules.rule: %mocks%
phpstan.rules.rule: %symplify.mocks%

# explicit expects()
Symplify\PHPStanRules\Rules\PHPUnit\ExplicitExpectsMockMethodRule:
phpstan.rules.rule: %mocks%
phpstan.rules.rule: %symplify.mocks%
Symplify\PHPStanRules\Rules\PHPUnit\AvoidAnyExpectsRule:
phpstan.rules.rule: %mocks%
phpstan.rules.rule: %symplify.mocks%
Symplify\PHPStanRules\Rules\PHPUnit\NoWithOnStubRule:
phpstan.rules.rule: %mocks%
phpstan.rules.rule: %symplify.mocks%
Symplify\PHPStanRules\Rules\PHPUnit\RequireAtLeastOneRule:
phpstan.rules.rule: %mocks%
phpstan.rules.rule: %symplify.mocks%

# better alternative than mocks
Symplify\PHPStanRules\Rules\Doctrine\NoDocumentMockingRule:
phpstan.rules.rule: %mocks%
phpstan.rules.rule: %symplify.mocks%
Symplify\PHPStanRules\Rules\Doctrine\NoEntityMockingRule:
phpstan.rules.rule: %mocks%
phpstan.rules.rule: %symplify.mocks%

services:
- Symplify\PHPStanRules\Rules\PHPUnit\NoMockOnlyTestRule
Expand Down
42 changes: 28 additions & 14 deletions config/phpstan-extensions.neon
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
# return type extensions are disabled by default; enable in your phpstan.neon with:
#
# parameters:
# symfonyReturnType: true
# laravelReturnType: true
# pathStrings: true

parameters:
symfonyReturnType: false
laravelReturnType: false
pathStrings: false
symplify:
mocks: false
ctor: false
symfonyReturnType: false
laravelReturnType: false
pathStrings: false
maximumIgnoredErrorCount: 0

# kept flat for backward compatibility, default to the %symplify.*% nested values
symfonyReturnType: %symplify.symfonyReturnType%
laravelReturnType: %symplify.laravelReturnType%
pathStrings: %symplify.pathStrings%

parametersSchema:
symplify: structure([
mocks: bool()
ctor: bool()
symfonyReturnType: bool()
laravelReturnType: bool()
pathStrings: bool()
maximumIgnoredErrorCount: int()
])

symfonyReturnType: bool()
laravelReturnType: bool()
pathStrings: bool()

conditionalTags:
# Symfony Container::get($1) => $1 type
Symplify\PHPStanRules\ReturnTypeExtension\Symfony\ContainerGetReturnTypeExtension:
phpstan.broker.dynamicMethodReturnTypeExtension: %symfonyReturnType%
phpstan.broker.dynamicMethodReturnTypeExtension: %symplify.symfonyReturnType%

# Symfony Finder SplFileInfo::getRealPath() => string type
Symplify\PHPStanRules\ReturnTypeExtension\Symfony\SplFileInfoTolerantReturnTypeExtension:
phpstan.broker.dynamicMethodReturnTypeExtension: %symfonyReturnType%
phpstan.broker.dynamicMethodReturnTypeExtension: %symplify.symfonyReturnType%

# Laravel Container::make($1) => $1 type
Symplify\PHPStanRules\ReturnTypeExtension\Laravel\LaravelContainerMakeTypeExtension:
phpstan.broker.dynamicMethodReturnTypeExtension: %laravelReturnType%
phpstan.broker.dynamicMethodReturnTypeExtension: %symplify.laravelReturnType%

# getcwd()/dirname()/realpath() => always "string"
Symplify\PHPStanRules\ReturnTypeExtension\NativeFunctionReturnTypeExtension:
phpstan.broker.dynamicFunctionReturnTypeExtension: %pathStrings%
phpstan.broker.dynamicFunctionReturnTypeExtension: %symplify.pathStrings%

services:
# use with "errorFormat: symplify" in CLI/config
Expand Down
6 changes: 1 addition & 5 deletions config/phpunit-rules.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
rules:
- Symplify\PHPStanRules\Rules\PHPUnit\PublicStaticDataProviderRule
- Symplify\PHPStanRules\Rules\PHPUnit\NoAssertFuncCallInTestsRule

# mocking rules moved to mock-rules.neon, enable them with:
#
# parameters:
# mocks: true
ě
3 changes: 2 additions & 1 deletion config/services/services.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
parameters:
# related to MaximumIgnoredErrorCountRule
maximumIgnoredErrorCount: 0
# kept flat for backward compatibility, defaults to %symplify.maximumIgnoredErrorCount%
maximumIgnoredErrorCount: %symplify.maximumIgnoredErrorCount%

parametersSchema:
# related to MaximumIgnoredErrorCountRule
Expand Down
4 changes: 3 additions & 1 deletion tests/ErrorFormatter/SymplifyErrorFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public static function provideData(): Iterator
#[Override]
public static function getAdditionalConfigFiles(): array
{
return [__DIR__ . '/../../config/phpstan-extensions.neon'];
return [
__DIR__ . '/../../config/phpstan-extensions.neon',
];
}
}
4 changes: 3 additions & 1 deletion tests/Rules/NewOverSettersRule/config/configured_rule.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
includes:
- ../../../../config/phpstan-extensions.neon
- ../../../../config/ctor-rules.neon

parameters:
ctor: true
symplify:
ctor: true
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
includes:
- ../../../../../../config/phpstan-extensions.neon
- ../../../../../../config/services/services.neon

rules:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
includes:
- ../../../../../../config/phpstan-extensions.neon
- ../../../../../../config/services/services.neon

rules:
Expand Down
1 change: 1 addition & 0 deletions tests/config/included_services.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
includes:
- ../../config/phpstan-extensions.neon
- ../../config/services/services.neon
Loading