Skip to content

Commit de1bad7

Browse files
committed
next rule
1 parent 487e8cd commit de1bad7

File tree

3 files changed

+192
-6
lines changed

3 files changed

+192
-6
lines changed

README.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,50 @@ class SomeCommand extends Command
112112

113113
<br>
114114

115+
### NoConstructorOverrideRule
116+
117+
Possible __construct() override, this can cause missing dependencies or setup
118+
119+
```yaml
120+
rules:
121+
- Symplify\PHPStanRules\Rules\NoConstructorOverrideRule
122+
```
123+
124+
```php
125+
class ParentClass
126+
{
127+
public function __construct(private string $dependency)
128+
{
129+
}
130+
}
131+
132+
class SomeClass extends ParentClass
133+
{
134+
public function __construct()
135+
{
136+
}
137+
}
138+
```
139+
140+
:x:
141+
142+
<br>
143+
144+
```php
145+
final class SomeClass extends ParentClass
146+
{
147+
public function __construct(private string $dependency)
148+
{
149+
}
150+
}
151+
```
152+
153+
:+1:
154+
155+
<br>
156+
157+
158+
115159
### ExplicitClassPrefixSuffixRule
116160

117161
Interface have suffix of "Interface", trait have "Trait" suffix exclusively
@@ -1380,9 +1424,149 @@ class SomeListener implements EventSubscriberInterface
13801424

13811425
:+1:
13821426

1427+
<br>
1428+
1429+
### RequireInvokableControllerRule
1430+
1431+
Use invokable controller with __invoke() method instead of named action method
1432+
1433+
```yaml
1434+
rules:
1435+
- Symplify\PHPStanRules\Rules\Symfony\RequireInvokableControllerRule
1436+
```
1437+
1438+
```php
1439+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1440+
use Symfony\Component\Routing\Annotation\Route;
1441+
1442+
final class SomeController extends AbstractController
1443+
{
1444+
#[Route()]
1445+
public function someMethod()
1446+
{
1447+
}
1448+
}
1449+
```
1450+
1451+
:x:
1452+
1453+
<br>
1454+
1455+
```php
1456+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1457+
1458+
final class SomeController extends AbstractController
1459+
{
1460+
#[Route()]
1461+
public function __invoke()
1462+
{
1463+
}
1464+
}
1465+
```
1466+
1467+
:+1:
1468+
1469+
<br>
1470+
1471+
---
1472+
1473+
<br>
1474+
1475+
## 3. PHPUnit-specific Rules
1476+
1477+
### NoEntityMockingRule, NoDocumentMockingRule
13831478

1479+
Instead of entity or document mocking, create object directly to get better type support
1480+
1481+
```yaml
1482+
rules:
1483+
- Symplify\PHPStanRules\Rules\PHPUnit\NoEntityMockingRule
1484+
- Symplify\PHPStanRules\Rules\PHPUnit\NoDocumentMockingRule
1485+
```
1486+
1487+
```php
1488+
use PHPUnit\Framework\TestCase;
1489+
1490+
final class SomeTest extends TestCase
1491+
{
1492+
public function test()
1493+
{
1494+
$someEntityMock = $this->createMock(SomeEntity::class);
1495+
}
1496+
}
1497+
```
13841498

1499+
:x:
13851500

13861501
<br>
13871502

1503+
```php
1504+
use PHPUnit\Framework\TestCase;
1505+
1506+
final class SomeTest extends TestCase
1507+
{
1508+
public function test()
1509+
{
1510+
$someEntityMock = new SomeEntity();
1511+
}
1512+
}
1513+
```
1514+
1515+
:+1:
1516+
1517+
<br>
1518+
1519+
### NoMockOnlyTestRule
1520+
1521+
Test should have at least one non-mocked property, to test something
1522+
1523+
```yaml
1524+
rules:
1525+
- Symplify\PHPStanRules\Rules\PHPUnit\NoMockOnlyTestRule
1526+
```
1527+
1528+
```php
1529+
use PHPUnit\Framework\MockObject\MockObject;
1530+
use PHPUnit\Framework\TestCase;
1531+
1532+
class SomeTest extends TestCase
1533+
{
1534+
private MockObject $firstMock;
1535+
private MockObject $secondMock;
1536+
1537+
public function setUp()
1538+
{
1539+
$this->firstMock = $this->createMock(SomeService::class);
1540+
$this->secondMock = $this->createMock(AnotherService::class);
1541+
}
1542+
}
1543+
```
1544+
1545+
:x:
1546+
1547+
<br>
1548+
1549+
```php
1550+
use PHPUnit\Framework\MockObject\MockObject;
1551+
use PHPUnit\Framework\TestCase;
1552+
1553+
class SomeTest extends TestCase
1554+
{
1555+
private SomeService $someService;
1556+
1557+
private FirstMock $firstMock;
1558+
1559+
public function setUp()
1560+
{
1561+
$this->someService = new SomeService();
1562+
$this->firstMock = $this->createMock(AnotherService::class);
1563+
}
1564+
}
1565+
```
1566+
1567+
:+1:
1568+
1569+
<br>
1570+
1571+
13881572
Happy coding!

src/Enum/ClassName.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,14 @@ final class ClassName
7070
* @var string
7171
*/
7272
public const DOCTRINE_FIXTURE_INTERFACE = 'Doctrine\Common\DataFixtures\FixtureInterface';
73+
7374
/**
7475
* @var string
7576
*/
7677
public const ENTITY_REPOSITORY_CLASS = 'Doctrine\ORM\EntityRepository';
78+
79+
/**
80+
* @var string
81+
*/
82+
public const MOCK_OBJECT_CLASS = 'PHPUnit\Framework\MockObject\MockObject';
7783
}

src/Rules/PHPUnit/NoMockOnlyTestRule.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Node\InClassNode;
1212
use PHPStan\Rules\Rule;
1313
use PHPStan\Rules\RuleErrorBuilder;
14+
use Symplify\PHPStanRules\Enum\ClassName;
1415
use Symplify\PHPStanRules\Enum\RuleIdentifier;
1516
use Symplify\PHPStanRules\Testing\PHPUnitTestAnalyser;
1617

@@ -26,11 +27,6 @@
2627
*/
2728
public const ERROR_MESSAGE = 'Test should have at least one non-mocked property, to test something';
2829

29-
/**
30-
* @var string
31-
*/
32-
private const MOCK_OBJECT_CLASS = 'PHPUnit\Framework\MockObject\MockObject';
33-
3430
public function getNodeType(): string
3531
{
3632
return InClassNode::class;
@@ -63,7 +59,7 @@ public function processNode(Node $node, Scope $scope): array
6359

6460
$propertyClassName = $property->type->toString();
6561

66-
if ($propertyClassName !== self::MOCK_OBJECT_CLASS) {
62+
if ($propertyClassName !== ClassName::MOCK_OBJECT_CLASS) {
6763
$hasExclusivelyMockedProperties = false;
6864
}
6965
}

0 commit comments

Comments
 (0)