Skip to content

Commit 7899231

Browse files
committed
Made respecting require/include once calls optional
1 parent 8577b95 commit 7899231

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

config.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<xs:attribute name="addParamDefaultToDocblockType" type="xs:boolean" default="false" />
4141
<xs:attribute name="allowFileIncludes" type="xs:boolean" default="true" />
4242
<xs:attribute name="ignoreIncludeSideEffects" type="xs:boolean" default="false" />
43+
<xs:attribute name="respectIncludeOnce" type="xs:boolean" default="false" />
4344
<xs:attribute name="allowStringToStandInForClass" type="xs:boolean" default="false" />
4445
<xs:attribute name="checkForThrowsDocblock" type="xs:boolean" default="false" />
4546
<xs:attribute name="checkForThrowsInGlobalScope" type="xs:boolean" default="false" />

docs/running_psalm/configuration.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,18 @@ Ignoring include side effects can significantly speed up scans on legacy codebas
489489

490490
Defaults to `false`.
491491

492+
#### respectIncludeOnce
493+
```xml
494+
<psalm
495+
respectIncludeOnce="[bool]"
496+
>
497+
```
498+
Whether or not to process files after the first `require_once()` or `include_once()` call.
499+
500+
By default, Psalm treats `require_once()` and `include_once()` calls as if they were `require()` or `include()` calls in an attempt to consider all possible permutations of file include order. Respecting `require_once()` and `include_once()` behavior can significantly speed up scans on codebases with circular `require_once()` or `include_once()` calls. While potentially less comprehensive, this setting aims to more closely mimic PHP's runtime behavior.
501+
502+
Defaults to `false`.
503+
492504
#### serializer
493505
```xml
494506
<psalm

src/Psalm/Config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ final class Config
304304

305305
public bool $ignore_include_side_effects = false;
306306

307+
public bool $respect_include_once = false;
308+
307309
/** @var 1|2|3|4|5|6|7|8 */
308310
public int $level = 1;
309311

@@ -954,6 +956,7 @@ private static function fromXmlAndPaths(
954956
'resolveFromConfigFile' => 'resolve_from_config_file',
955957
'allowFileIncludes' => 'allow_includes',
956958
'ignoreIncludeSideEffects' => 'ignore_include_side_effects',
959+
'respectIncludeOnce' => 'respect_include_once',
957960
'strictBinaryOperands' => 'strict_binary_operands',
958961
'allowBoolToLiteralBoolComparison' => 'allow_bool_to_literal_bool_comparison',
959962
'rememberPropertyAssignmentsAfterCall' => 'remember_property_assignments_after_call',

src/Psalm/Internal/Analyzer/Statements/Expression/IncludeAnalyzer.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,15 @@ public static function analyze(
175175
$statements_analyzer->hasAlreadyRequiredFilePath($path_to_file)
176176
&& (
177177
!$codebase->file_storage_provider->get($path_to_file)->has_extra_statements
178-
|| in_array($stmt->type, [
179-
PhpParser\Node\Expr\Include_::TYPE_INCLUDE_ONCE,
180-
PhpParser\Node\Expr\Include_::TYPE_REQUIRE_ONCE,
181-
])
178+
||
179+
(
180+
$config->respect_include_once
181+
&&
182+
in_array($stmt->type, [
183+
PhpParser\Node\Expr\Include_::TYPE_INCLUDE_ONCE,
184+
PhpParser\Node\Expr\Include_::TYPE_REQUIRE_ONCE,
185+
])
186+
)
182187
)
183188
)
184189
) {

0 commit comments

Comments
 (0)