Skip to content

Commit 30b6e66

Browse files
committed
Disallow dynamic calls to static methods
1 parent dddd440 commit 30b6e66

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

src/Ruleset/Nexus73.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public function __construct()
291291
'php_unit_size_class' => false,
292292
'php_unit_strict' => true, // risky
293293
'php_unit_test_annotation' => false, // risky
294-
'php_unit_test_case_static_method_calls' => ['call_type' => 'this'], // risky
294+
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'], // risky
295295
'php_unit_test_class_requires_covers' => false,
296296
'phpdoc_add_missing_param_annotation' => ['only_untyped' => false],
297297
'phpdoc_align' => true,

src/Test/AbstractRulesetTestCase.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ final public function testAllConfiguredRulesAreBuiltIn(): void
5252
sort($fixersNotBuiltIn);
5353
$c = \count($fixersNotBuiltIn);
5454

55-
$this->assertEmpty($fixersNotBuiltIn, sprintf(
55+
self::assertEmpty($fixersNotBuiltIn, sprintf(
5656
'Failed asserting that %s for the %s "%s" %s built-in to PhpCsFixer.',
5757
$c > 1 ? 'fixers' : 'fixer',
5858
$c > 1 ? 'rules' : 'rule',
@@ -74,7 +74,7 @@ final public function testRulesAreSortedByName(string $source, array $rules): vo
7474
$sorted = $rules;
7575
sort($sorted);
7676

77-
$this->assertSame($sorted, $rules, sprintf(
77+
self::assertSame($sorted, $rules, sprintf(
7878
'Failed asserting that the rules in "%s" are sorted by name.',
7979
$source
8080
));
@@ -90,7 +90,7 @@ final public function testAllBuiltInRulesAreConfigured(): void
9090
sort($fixersWithoutConfiguration);
9191
$c = \count($fixersWithoutConfiguration);
9292

93-
$this->assertEmpty($fixersWithoutConfiguration, sprintf(
93+
self::assertEmpty($fixersWithoutConfiguration, sprintf(
9494
'Failed asserting that built-in %s for the %s "%s" %s configured in this ruleset.',
9595
$c > 1 ? 'fixers' : 'fixer',
9696
$c > 1 ? 'rules' : 'rule',
@@ -103,8 +103,8 @@ final public function testHeaderCommentFixerIsDisabledByDefault(): void
103103
{
104104
$rules = self::createRuleset()->getRules();
105105

106-
$this->assertArrayHasKey('header_comment', $rules);
107-
$this->assertFalse($rules['header_comment']);
106+
self::assertArrayHasKey('header_comment', $rules);
107+
self::assertFalse($rules['header_comment']);
108108
}
109109

110110
/**

strict-rules.neon.dist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# From phpstan/phpstan-strict-rules
2-
# but removed overly strict rules
2+
# but commented overly strict rules
33

44
parameters:
55
polluteScopeWithLoopInitialAssignments: false
@@ -23,6 +23,7 @@ rules:
2323
- PHPStan\Rules\Classes\RequireParentConstructCallRule
2424
- PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule
2525
- PHPStan\Rules\DisallowedConstructs\DisallowedImplicitArrayCreationRule
26+
# - PHPStan\Rules\DisallowedConstructs\DisallowedShortTernaryRule
2627
- PHPStan\Rules\ForeachLoop\OverwriteVariablesWithForeachRule
2728
- PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule
2829
- PHPStan\Rules\Operators\OperandInArithmeticPostDecrementRule
@@ -35,6 +36,7 @@ rules:
3536
- PHPStan\Rules\Operators\OperandsInArithmeticModuloRule
3637
- PHPStan\Rules\Operators\OperandsInArithmeticMultiplicationRule
3738
- PHPStan\Rules\Operators\OperandsInArithmeticSubtractionRule
39+
- PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsRule
3840
- PHPStan\Rules\StrictCalls\StrictFunctionCallsRule
3941
- PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule
4042
- PHPStan\Rules\VariableVariables\VariableMethodCallRule

tests/FactoryTest.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,45 +44,45 @@ public function testFactoryThrowsExceptionOnIncompatibleVersionId(): void
4444
public function testFactoryReturnsInstanceOfConfig(): void
4545
{
4646
$config = Factory::create($this->mockRuleset())->forProjects();
47-
$this->assertInstanceOf('PhpCsFixer\Config', $config);
47+
self::assertInstanceOf('PhpCsFixer\Config', $config);
4848

4949
$config = Factory::create($this->mockRuleset())->forLibrary('Library', 'John Doe', 'email', 2020);
50-
$this->assertInstanceOf('PhpCsFixer\Config', $config);
50+
self::assertInstanceOf('PhpCsFixer\Config', $config);
5151
}
5252

5353
public function testFactoryPassesSameRulesFromRuleset(): void
5454
{
5555
$ruleset = $this->mockRuleset();
5656
$config = Factory::create($ruleset)->forProjects();
5757

58-
$this->assertSame($ruleset->getRules(), $config->getRules());
58+
self::assertSame($ruleset->getRules(), $config->getRules());
5959
}
6060

6161
public function testFactoryAllowsOverrideOfRules(): void
6262
{
6363
$config = Factory::create(new Nexus73())->forProjects();
64-
$this->assertIsArray($config->getRules()['binary_operator_spaces']);
64+
self::assertIsArray($config->getRules()['binary_operator_spaces']);
6565

6666
$config = Factory::create(new Nexus73(), [
6767
'binary_operator_spaces' => false,
6868
])->forProjects();
69-
$this->assertFalse($config->getRules()['binary_operator_spaces']);
69+
self::assertFalse($config->getRules()['binary_operator_spaces']);
7070
}
7171

7272
public function testFactoryReturnsDefaultOptionsWhenNoOptionsGiven(): void
7373
{
7474
$config = Factory::create($this->mockRuleset())->forProjects();
7575

76-
$this->assertSame('.php_cs.cache', $config->getCacheFile());
77-
$this->assertSame([], $config->getCustomFixers());
78-
$this->assertInstanceOf('PhpCsFixer\Finder', $config->getFinder());
79-
$this->assertSame('txt', $config->getFormat());
80-
$this->assertFalse($config->getHideProgress());
81-
$this->assertSame(' ', $config->getIndent());
82-
$this->assertSame("\n", $config->getLineEnding());
83-
$this->assertNull($config->getPhpExecutable());
84-
$this->assertIsBool($config->getRiskyAllowed());
85-
$this->assertTrue($config->getUsingCache());
76+
self::assertSame('.php_cs.cache', $config->getCacheFile());
77+
self::assertSame([], $config->getCustomFixers());
78+
self::assertInstanceOf('PhpCsFixer\Finder', $config->getFinder());
79+
self::assertSame('txt', $config->getFormat());
80+
self::assertFalse($config->getHideProgress());
81+
self::assertSame(' ', $config->getIndent());
82+
self::assertSame("\n", $config->getLineEnding());
83+
self::assertNull($config->getPhpExecutable());
84+
self::assertIsBool($config->getRiskyAllowed());
85+
self::assertTrue($config->getUsingCache());
8686
}
8787

8888
public function testFactoryConsumesPassedOptionsToIt(): void
@@ -98,22 +98,22 @@ public function testFactoryConsumesPassedOptionsToIt(): void
9898
];
9999
$config = Factory::create($this->mockRuleset(), [], $options)->forProjects();
100100

101-
$this->assertSame($options['cacheFile'], $config->getCacheFile());
102-
$this->assertSame($options['format'], $config->getFormat());
103-
$this->assertTrue($config->getHideProgress());
104-
$this->assertSame($options['indent'], $config->getIndent());
105-
$this->assertSame($options['lineEnding'], $config->getLineEnding());
106-
$this->assertSame($options['phpExecutable'], $config->getPhpExecutable());
107-
$this->assertFalse($config->getUsingCache());
101+
self::assertSame($options['cacheFile'], $config->getCacheFile());
102+
self::assertSame($options['format'], $config->getFormat());
103+
self::assertTrue($config->getHideProgress());
104+
self::assertSame($options['indent'], $config->getIndent());
105+
self::assertSame($options['lineEnding'], $config->getLineEnding());
106+
self::assertSame($options['phpExecutable'], $config->getPhpExecutable());
107+
self::assertFalse($config->getUsingCache());
108108
}
109109

110110
public function testCreateForLibraryCreatesPreformattedLicense(): void
111111
{
112112
$config = Factory::create($this->mockRuleset())->forLibrary('Library', 'Foo Bar', '[email protected]', 2020);
113113
$header = $config->getRules()['header_comment']['header'];
114114

115-
$this->assertStringContainsString('This file is part of Library.', $header);
116-
$this->assertStringContainsString('(c) 2020 Foo Bar <[email protected]>', $header);
115+
self::assertStringContainsString('This file is part of Library.', $header);
116+
self::assertStringContainsString('(c) 2020 Foo Bar <[email protected]>', $header);
117117
}
118118

119119
/**

0 commit comments

Comments
 (0)