Skip to content
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
9 changes: 9 additions & 0 deletions src/Analyser/ExprHandler/Helper/DefaultNarrowingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public function __construct(
#[AutowiredParameter]
private bool $rememberPossiblyImpureFunctionValues,
private ReflectionProvider $reflectionProvider,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
)
{
}
Expand Down Expand Up @@ -655,6 +657,13 @@ public function createIssetSingleSubjectNonTrueTypes(
)->setRootExpr($rootExpr);

if ($issetExpr instanceof Expr\Variable && is_string($issetExpr->name)) {
// the variable's existence is shared by the PHPDoc and native types:
// with uncertain PHPDoc types, a variable whose native type allows
// null still exists in !isset()
if (!$isNullable && !$this->treatPhpDocTypesAsCertain) {
$isNullable = !$varResult->getTypeOnScope($s, true)->isNull()->no();
}

if ($isset === true) {
if ($isNullable) {
return $exprType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Variables;

use PHPStan\Rules\NonStringableDynamicAccessCheck;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
use function array_merge;

/**
* @extends RuleTestCase<DefinedVariableRule>
*/
class DefinedVariableRuleWithUncertainPhpDocTypesTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new DefinedVariableRule(
new NonStringableDynamicAccessCheck(
new RuleLevelHelper(
self::createReflectionProvider(),
checkNullables: true,
checkThisOnly: false,
checkUnionTypes: true,
checkExplicitMixed: false,
checkImplicitMixed: false,
checkBenevolentUnionTypes: false,
discoveringSymbolsTip: true,
),
false,
),
cliArgumentsVariablesRegistered: true,
checkMaybeUndefinedVariables: true,
);
}

public static function getAdditionalConfigFiles(): array
{
return array_merge(
parent::getAdditionalConfigFiles(),
[
__DIR__ . '/../uncertain-phpdoc-types.neon',
],
);
}

protected function shouldTreatPhpDocTypesAsCertain(): bool
{
return false;
}

public function testBug15728(): void
{
$this->analyse([__DIR__ . '/data/bug-15728.php'], [
[
'Variable $type might not be defined.',
42,
],
]);
}

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

namespace Bug15728;

/** @return int */
function getInt(string $key)
{
return (int) $key;
}

function viaTernary(): void
{
$type = getInt('type');
echo isset($type) ? 'set' : 'null';
echo $type;
}

/** @param float $x */
function viaTernaryOnParameter($x = 0): void
{
echo isset($x) ? 'set' : 'null';
echo $x;
}

function viaIfElse(): void
{
$type = getInt('type');
if (isset($type)) {
echo 'set';
} else {
echo $type;
}
echo $type;
}

function maybeDefined(bool $b): void
{
if ($b) {
$type = getInt('type');
}
if (!isset($type)) {
echo $type;
}
}
22 changes: 18 additions & 4 deletions turbo-ext/src/DefaultNarrowingHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,13 @@ class DefaultNarrowingHelper
explicit DefaultNarrowingHelper(zend_object *self) : self(self) {}

/* the constructor body: the promoted properties */
void construct(zval *exprPrinter, bool rememberPossiblyImpureFunctionValues, zval *reflectionProvider)
void construct(zval *exprPrinter, bool rememberPossiblyImpureFunctionValues, zval *reflectionProvider, bool treatPhpDocTypesAsCertain)
{
zv::ObjRef object(self);
object.propAtWrite(slots::exprPrinter, zv::Val::copyOf(zv::Ref(exprPrinter)));
object.propAtWrite(slots::rememberPossiblyImpureFunctionValues, zv::Val::boolean(rememberPossiblyImpureFunctionValues));
object.propAtWrite(slots::reflectionProvider, zv::Val::copyOf(zv::Ref(reflectionProvider)));
object.propAtWrite(slots::treatPhpDocTypesAsCertain, zv::Val::boolean(treatPhpDocTypesAsCertain));
}

/* Mirrors specifyTypesForNode(). */
Expand Down Expand Up @@ -1007,6 +1008,17 @@ class DefaultNarrowingHelper
if (UNEXPECTED(exprType.isUndef())) return zv::Val();

if (isA(issetExpr, PT_CLASS_VARIABLE) && Z_TYPE_P(nameOf(issetExpr)) == IS_STRING) {
// the variable's existence is shared by the PHPDoc and native types:
// with uncertain PHPDoc types, a variable whose native type allows
// null still exists in !isset()
if (!isNullable && !treatPhpDocTypesAsCertain()) {
zv::Val nativeType = pt_expression_result_get_type_on_scope(varResult, s, true);
if (UNEXPECTED(nativeType.isUndef())) return zv::Val();
zend_long nativeTypeIsNull = typeTrinary(nativeType.raw(), PT_OP_IS_NULL, "isNull");
if (UNEXPECTED(nativeTypeIsNull < 0)) return zv::Val();
isNullable = nativeTypeIsNull != PT_TRI_NO;
}

if (isset == IS_TRUE) {
if (isNullable) return exprType;

Expand Down Expand Up @@ -1369,6 +1381,8 @@ class DefaultNarrowingHelper

bool rememberPossiblyImpureFunctionValues() const { return Z_TYPE_P(OBJ_PROP_NUM(self, slots::rememberPossiblyImpureFunctionValues)) == IS_TRUE; }

bool treatPhpDocTypesAsCertain() const { return Z_TYPE_P(OBJ_PROP_NUM(self, slots::treatPhpDocTypesAsCertain)) == IS_TRUE; }

/* $this->exprPrinter->printExpr($expr); NULL = pending exception */
zv::Str print(zval *expr) const
{
Expand Down Expand Up @@ -2234,9 +2248,9 @@ PT_MINIT_REGISTRATION(pt_register_default_narrowing_helper)
* #[AutowiredParameter] by name) */
cls.method(sigs::__construct, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *exprPrinter, *reflectionProvider;
bool rememberPossiblyImpureFunctionValues;
if (!zp::parse<zp::Obj, zp::Bool, zp::Obj>(execute_data, exprPrinter, rememberPossiblyImpureFunctionValues, reflectionProvider)) RETURN_THROWS();
PT_DNH_THIS.construct(exprPrinter, rememberPossiblyImpureFunctionValues, reflectionProvider);
bool rememberPossiblyImpureFunctionValues, treatPhpDocTypesAsCertain;
if (!zp::parse<zp::Obj, zp::Bool, zp::Obj, zp::Bool>(execute_data, exprPrinter, rememberPossiblyImpureFunctionValues, reflectionProvider, treatPhpDocTypesAsCertain)) RETURN_THROWS();
PT_DNH_THIS.construct(exprPrinter, rememberPossiblyImpureFunctionValues, reflectionProvider, treatPhpDocTypesAsCertain);
});

cls.method(sigs::specifyTypesForNode, [](INTERNAL_FUNCTION_PARAMETERS) {
Expand Down
Loading
Loading