Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/Analyser/ExprHandler/Helper/DefaultNarrowingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ static function (string $parameterName) use ($argsMap, $getArgType): ?Type {
static function (Type $type, callable $traverse) use ($templateTypeMap, &$containsUnresolvedTemplate) {
if ($type instanceof TemplateType && $type->getScope()->getClassName() !== null) {
$resolvedType = $templateTypeMap->getType($type->getName());
if ($resolvedType === null || $type->getBound()->equals($resolvedType)) {
if ($resolvedType === null || ($type->getScope()->getFunctionName() === null && $type->getBound()->equals($resolvedType))) {
$containsUnresolvedTemplate = true;
return $type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,38 @@ public function testAssertUnresolvedGeneric(): void
$this->analyse([__DIR__ . '/data/assert-unresolved-generic.php'], []);
}

#[RequiresPhp('>= 8.0.0')]
public function testNoNamedArgumentsAssertIsAlwaysSatisfied(): void
{
$this->treatPhpDocTypesAsCertain = false;
$this->analyse([__DIR__ . '/data/no-named-arguments-assert.php'], [
[
'Call to static method NoNamedArgumentsAssert\QueryBuilder::validateVariadicParameter() with list<mixed> will always evaluate to true.',
14,
],
[
'Call to static method NoNamedArgumentsAssert\QueryBuilder::validateVariadicParameter() with list<string> will always evaluate to true.',
22,
],
]);
}

#[RequiresPhp('>= 8.0.0')]
public function testNoNamedArgumentsAssertIsAlwaysSatisfiedTreatPhpDocTypesAsCertain(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/no-named-arguments-assert.php'], [
[
'Call to static method NoNamedArgumentsAssert\QueryBuilder::validateVariadicParameter() with list<mixed> will always evaluate to true.',
14,
],
[
'Call to static method NoNamedArgumentsAssert\QueryBuilder::validateVariadicParameter() with list<string> will always evaluate to true.',
22,
],
]);
}

public static function dataReportAlwaysTrueInLastCondition(): iterable
{
yield [false, [
Expand Down
39 changes: 39 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/no-named-arguments-assert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php // lint >= 8.0
declare(strict_types = 1);

namespace NoNamedArgumentsAssert;

final class QueryBuilder
{

/**
* @no-named-arguments
*/
public function select(mixed ...$select): void
{
self::validateVariadicParameter($select);
}

/**
* @no-named-arguments
*/
public function groupBy(string ...$groupBy): void
{
self::validateVariadicParameter($groupBy);
}

/**
* @template TItem
* @param TItem[] $parameter
* @phpstan-assert list<TItem> $parameter
*/
private static function validateVariadicParameter(array $parameter): void
{
if (array_is_list($parameter)) {
return;
}

throw new \InvalidArgumentException();
}

}
20 changes: 12 additions & 8 deletions turbo-ext/src/DefaultNarrowingHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2027,15 +2027,19 @@ class DefaultNarrowingHelper
if (UNEXPECTED(resolvedType.isUndef())) return;
bool unresolved = resolvedType.isNull();
if (!unresolved) {
zv::Val bound = pt_type_call(Z_OBJ_P(type), PT_LC("getbound"), 0, NULL);
if (UNEXPECTED(bound.isUndef())) return;
if (UNEXPECTED(Z_TYPE_P(bound.raw()) != IS_OBJECT)) {
(void) callOnNonObject("equals", bound.raw());
return;
zv::Val functionName = pt_type_call(Z_OBJ_P(templateScope.raw()), PT_LC("getfunctionname"), 0, NULL);
if (UNEXPECTED(functionName.isUndef())) return;
if (functionName.isNull()) {
zv::Val bound = pt_type_call(Z_OBJ_P(type), PT_LC("getbound"), 0, NULL);
if (UNEXPECTED(bound.isUndef())) return;
if (UNEXPECTED(Z_TYPE_P(bound.raw()) != IS_OBJECT)) {
(void) callOnNonObject("equals", bound.raw());
return;
}
zv::Val equals = pt_type_op(Z_OBJ_P(bound.raw()), PT_OP_EQUALS, 1, resolvedType.raw());
if (UNEXPECTED(equals.isUndef())) return;
unresolved = zend_is_true(equals.raw());
}
zv::Val equals = pt_type_op(Z_OBJ_P(bound.raw()), PT_OP_EQUALS, 1, resolvedType.raw());
if (UNEXPECTED(equals.isUndef())) return;
unresolved = zend_is_true(equals.raw());
}
if (unresolved) {
zval *target = containsUnresolvedTemplate;
Expand Down
Loading