Skip to content

Use explicit mixed for global array variables #1411

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

Merged
merged 3 commits into from
Jun 13, 2022
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
1 change: 1 addition & 0 deletions conf/bleedingEdge.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ parameters:
bleedingEdge: true
skipCheckGenericClasses: []
explicitMixedInUnknownGenericNew: true
explicitMixedForGlobalVariables: true
explicitMixedViaIsArray: true
arrayFilter: true
arrayUnpacking: true
Expand Down
2 changes: 2 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ parameters:
- RecursiveCallbackFilterIterator
- AppendIterator
explicitMixedInUnknownGenericNew: false
explicitMixedForGlobalVariables: false
explicitMixedViaIsArray: false
arrayFilter: false
arrayUnpacking: false
Expand Down Expand Up @@ -228,6 +229,7 @@ parametersSchema:
disableRuntimeReflectionProvider: bool(),
skipCheckGenericClasses: listOf(string()),
explicitMixedInUnknownGenericNew: bool(),
explicitMixedForGlobalVariables: bool(),
explicitMixedViaIsArray: bool(),
arrayFilter: bool(),
arrayUnpacking: bool(),
Expand Down
2 changes: 2 additions & 0 deletions src/Analyser/DirectScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function __construct(
private bool $treatPhpDocTypesAsCertain,
private PhpVersion $phpVersion,
private bool $explicitMixedInUnknownGenericNew,
private bool $explicitMixedForGlobalVariables,
private ConstantResolver $constantResolver,
)
{
Expand Down Expand Up @@ -110,6 +111,7 @@ public function create(
$afterExtractCall,
$parentScope,
$this->explicitMixedInUnknownGenericNew,
$this->explicitMixedForGlobalVariables,
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Analyser/LazyScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class LazyScopeFactory implements ScopeFactory

private bool $explicitMixedInUnknownGenericNew;

private bool $explicitMixedForGlobalVariables;

/**
* @param class-string $scopeClass
*/
Expand All @@ -33,6 +35,7 @@ public function __construct(
{
$this->treatPhpDocTypesAsCertain = $container->getParameter('treatPhpDocTypesAsCertain');
$this->explicitMixedInUnknownGenericNew = $this->container->getParameter('featureToggles')['explicitMixedInUnknownGenericNew'];
$this->explicitMixedForGlobalVariables = $this->container->getParameter('featureToggles')['explicitMixedForGlobalVariables'];
}

/**
Expand Down Expand Up @@ -102,6 +105,7 @@ public function create(
$afterExtractCall,
$parentScope,
$this->explicitMixedInUnknownGenericNew,
$this->explicitMixedForGlobalVariables,
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public function __construct(
private bool $afterExtractCall = false,
private ?Scope $parentScope = null,
private bool $explicitMixedInUnknownGenericNew = false,
private bool $explicitMixedForGlobalVariables = false,
)
{
if ($namespace === '') {
Expand Down Expand Up @@ -495,7 +496,7 @@ public function hasVariableType(string $variableName): TrinaryLogic
public function getVariableType(string $variableName): Type
{
if ($this->isGlobalVariable($variableName)) {
return new ArrayType(new StringType(), new MixedType());
return new ArrayType(new StringType(), new MixedType($this->explicitMixedForGlobalVariables));
}

if ($this->hasVariableType($variableName)->no()) {
Expand Down
1 change: 1 addition & 0 deletions src/Testing/PHPStanTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public function createScopeFactory(ReflectionProvider $reflectionProvider, TypeS
$this->shouldTreatPhpDocTypesAsCertain(),
$container->getByType(PhpVersion::class),
$container->getParameter('featureToggles')['explicitMixedInUnknownGenericNew'],
$container->getParameter('featureToggles')['explicitMixedForGlobalVariables'],
$constantResolver,
);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1077,4 +1077,25 @@ public function testBug6987(): void
$this->analyse([__DIR__ . '/data/bug-6987.php'], []);
}

public function testDiscussion7450WithoutCheckExplicitMixed(): void
{
$this->checkExplicitMixed = false;
$this->analyse([__DIR__ . '/data/discussion-7450.php'], []);
}

public function testDiscussion7450WithCheckExplicitMixed(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/discussion-7450.php'], [
[
'Parameter #1 $foo of function Discussion7450\foo expects array{policy: non-empty-string, entitlements: array<non-empty-string>}, array{policy: mixed, entitlements: mixed} given.',
18,
],
[
'Parameter #1 $foo of function Discussion7450\foo expects array{policy: non-empty-string, entitlements: array<non-empty-string>}, array{policy: mixed, entitlements: mixed} given.',
28,
],
]);
}

}
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Functions/data/discussion-7450.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Discussion7450;

use function PHPStan\Testing\assertType;

/** @param array{policy: non-empty-string, entitlements: non-empty-string[]} $foo */
function foo(array $foo): void
{
}

$args = [
'policy' => $_POST['policy'], // shouldn't this and the next line be an unsafe offset access?
'entitlements' => $_POST['entitlements'],
];
assertType('mixed', $_POST['policy']);
assertType('array{policy: mixed, entitlements: mixed}', $args);
foo($args); // I'd expect this to be reported too

/** @var mixed $mixed */
$mixed = null;
$args = [
'policy' => $mixed,
'entitlements' => $mixed,
];
assertType('mixed', $mixed);
assertType('array{policy: mixed, entitlements: mixed}', $args);
foo($args);