Skip to content

Commit

Permalink
Bleeding edge - new $string leads to object, not mixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 21, 2021
1 parent fa9ee15 commit 78a9f05
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions conf/bleedingEdge.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ parameters:
checkMissingTemplateTypeInParameter: true
wrongVarUsage: true
arrayDestructuring: true
objectFromNewClass: true
4 changes: 3 additions & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ parameters:
checkMissingTemplateTypeInParameter: false
wrongVarUsage: false
arrayDestructuring: false
objectFromNewClass: false
fileExtensions:
- php
checkAlwaysTrueCheckTypeFunctionCall: false
Expand Down Expand Up @@ -180,7 +181,8 @@ parametersSchema:
checkLogicalOrConstantCondition: bool(),
checkMissingTemplateTypeInParameter: bool(),
wrongVarUsage: bool(),
arrayDestructuring: bool()
arrayDestructuring: bool(),
objectFromNewClass: bool()
])
fileExtensions: listOf(string())
checkAlwaysTrueCheckTypeFunctionCall: bool()
Expand Down
5 changes: 5 additions & 0 deletions src/Analyser/DirectScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class DirectScopeFactory implements ScopeFactory

private bool $treatPhpDocTypesAsCertain;

private bool $objectFromNewClass;

/** @var string[] */
private array $dynamicConstantNames;

Expand All @@ -50,6 +52,7 @@ public function __construct(
\PHPStan\Parser\Parser $parser,
NodeScopeResolver $nodeScopeResolver,
bool $treatPhpDocTypesAsCertain,
bool $objectFromNewClass,
Container $container
)
{
Expand All @@ -63,6 +66,7 @@ public function __construct(
$this->parser = $parser;
$this->nodeScopeResolver = $nodeScopeResolver;
$this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain;
$this->objectFromNewClass = $objectFromNewClass;
$this->dynamicConstantNames = $container->getParameter('dynamicConstantNames');
}

Expand Down Expand Up @@ -136,6 +140,7 @@ public function create(
$inFunctionCallsStack,
$this->dynamicConstantNames,
$this->treatPhpDocTypesAsCertain,
$this->objectFromNewClass,
$afterExtractCall,
$parentScope
);
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 $treatPhpDocTypesAsCertain;

private bool $objectFromNewClass;

public function __construct(
string $scopeClass,
Container $container
Expand All @@ -32,6 +34,7 @@ public function __construct(
$this->container = $container;
$this->dynamicConstantNames = $container->getParameter('dynamicConstantNames');
$this->treatPhpDocTypesAsCertain = $container->getParameter('treatPhpDocTypesAsCertain');
$this->objectFromNewClass = $container->getParameter('featureToggles')['objectFromNewClass'];
}

/**
Expand Down Expand Up @@ -104,6 +107,7 @@ public function create(
$inFunctionCallsStack,
$this->dynamicConstantNames,
$this->treatPhpDocTypesAsCertain,
$this->objectFromNewClass,
$afterExtractCall,
$parentScope
);
Expand Down
14 changes: 14 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ class MutatingScope implements Scope

private bool $treatPhpDocTypesAsCertain;

private bool $objectFromNewClass;

private bool $afterExtractCall;

private ?Scope $parentScope;
Expand Down Expand Up @@ -195,6 +197,7 @@ class MutatingScope implements Scope
* @param array<MethodReflection|FunctionReflection> $inFunctionCallsStack
* @param string[] $dynamicConstantNames
* @param bool $treatPhpDocTypesAsCertain
* @param bool $objectFromNewClass
* @param bool $afterExtractCall
* @param Scope|null $parentScope
*/
Expand Down Expand Up @@ -224,6 +227,7 @@ public function __construct(
array $inFunctionCallsStack = [],
array $dynamicConstantNames = [],
bool $treatPhpDocTypesAsCertain = true,
bool $objectFromNewClass = false,
bool $afterExtractCall = false,
?Scope $parentScope = null
)
Expand Down Expand Up @@ -257,6 +261,7 @@ public function __construct(
$this->inFunctionCallsStack = $inFunctionCallsStack;
$this->dynamicConstantNames = $dynamicConstantNames;
$this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain;
$this->objectFromNewClass = $objectFromNewClass;
$this->afterExtractCall = $afterExtractCall;
$this->parentScope = $parentScope;
}
Expand Down Expand Up @@ -2204,6 +2209,7 @@ public function doNotTreatPhpDocTypesAsCertain(): Scope
$this->inFunctionCallsStack,
$this->dynamicConstantNames,
false,
$this->objectFromNewClass,
$this->afterExtractCall,
$this->parentScope
);
Expand Down Expand Up @@ -4641,6 +4647,10 @@ private function getTypeToInstantiateForNew(Type $type): Type
foreach ($type->getTypes() as $innerType) {
$decidedType = $decideType($innerType);
if ($decidedType === null) {
if ($this->objectFromNewClass) {
return new ObjectWithoutClassType();
}

return new MixedType(false, new StringType());
}

Expand All @@ -4652,6 +4662,10 @@ private function getTypeToInstantiateForNew(Type $type): Type

$decidedType = $decideType($type);
if ($decidedType === null) {
if ($this->objectFromNewClass) {
return new ObjectWithoutClassType();
}

return new MixedType(false, new StringType());
}

Expand Down
1 change: 1 addition & 0 deletions src/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ public function createScopeFactory(Broker $broker, TypeSpecifier $typeSpecifier)
$this->getParser(),
self::getContainer()->getByType(NodeScopeResolver::class),
$this->shouldTreatPhpDocTypesAsCertain(),
false,
$container
);
}
Expand Down

0 comments on commit 78a9f05

Please sign in to comment.