Skip to content

Commit

Permalink
Merge branch 'fix_weakmap' into fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Dec 8, 2023
2 parents 893f2a0 + f980689 commit 5f977d6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -577,23 +577,23 @@ public static function replaceTemplateTypes(
) {
if ($template_type->param_name === 'TFunctionArgCount') {
$template_result->lower_bounds[$template_type->param_name] = [
'fn-' . strtolower((string) $method_id) => [
'fn-' . $method_id->method_name => [
new TemplateBound(
Type::getInt(false, $arg_count),
),
],
];
} elseif ($template_type->param_name === 'TPhpMajorVersion') {
$template_result->lower_bounds[$template_type->param_name] = [
'fn-' . strtolower((string) $method_id) => [
'fn-' . $method_id->method_name => [
new TemplateBound(
Type::getInt(false, $codebase->getMajorAnalysisPhpVersion()),
),
],
];
} elseif ($template_type->param_name === 'TPhpVersionId') {
$template_result->lower_bounds[$template_type->param_name] = [
'fn-' . strtolower((string) $method_id) => [
'fn-' . $method_id->method_name => [
new TemplateBound(
Type::getInt(
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ private static function resolveTemplateResultLowerBound(
): array {
if ($template_type->param_name === 'TFunctionArgCount') {
return [
'fn-' . strtolower((string)$method_id) => [
'fn-' . $method_id->method_name => [
new TemplateBound(
Type::getInt(false, count($stmt->getArgs())),
),
Expand All @@ -640,7 +640,7 @@ private static function resolveTemplateResultLowerBound(

if ($template_type->param_name === 'TPhpMajorVersion') {
return [
'fn-' . strtolower((string)$method_id) => [
'fn-' . $method_id->method_name => [
new TemplateBound(
Type::getInt(false, $codebase->getMajorAnalysisPhpVersion()),
),
Expand All @@ -650,7 +650,7 @@ private static function resolveTemplateResultLowerBound(

if ($template_type->param_name === 'TPhpVersionId') {
return [
'fn-' . strtolower((string) $method_id) => [
'fn-' . $method_id->method_name => [
new TemplateBound(
Type::getInt(
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,8 @@ private static function getConditionalSanitizedTypeTokens(

if ($token_body === 'func_num_args()') {
$template_name = 'TFunctionArgCount';

$storage->template_types[$template_name] = [
$template_function_id => Type::getInt(),
'fn-' . strtolower($storage->cased_name ?? '') => Type::getInt(),
];

$function_template_types[$template_name]
Expand All @@ -530,7 +529,7 @@ private static function getConditionalSanitizedTypeTokens(
$template_name = 'TPhpMajorVersion';

$storage->template_types[$template_name] = [
$template_function_id => Type::getInt(),
'fn-' . strtolower($storage->cased_name ?? '') => Type::getInt(),
];

$function_template_types[$template_name]
Expand All @@ -543,7 +542,7 @@ private static function getConditionalSanitizedTypeTokens(
$template_name = 'TPhpVersionId';

$storage->template_types[$template_name] = [
$template_function_id => Type::getInt(),
'fn-' . strtolower($storage->cased_name ?? '') => Type::getInt(),
];

$function_template_types[$template_name]
Expand Down
10 changes: 10 additions & 0 deletions stubs/CoreGenericClasses.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,16 @@ final class WeakMap implements ArrayAccess, Countable, IteratorAggregate, Traver
* @return void
*/
public function offsetUnset($offset) {}

/**
* Create a new iterator from an ArrayObject instance
* @link http://php.net/manual/en/arrayobject.getiterator.php
*
* @return \Traversable<TKey, TValue> An iterator from an ArrayObject.
*
* @since 5.0.0
*/
public function getIterator() { }
}

class mysqli
Expand Down
58 changes: 58 additions & 0 deletions tests/Template/ConditionalReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,31 @@ function zeroArgsFalseOneArgString(string $s = "") {
'$c' => 'string',
],
],
'InheritFuncNumArgs' => [
'code' => '<?php
abstract class A
{
/**
* @psalm-return (func_num_args() is 1 ? string : int)
*/
public static function get(bool $a, ?bool $b = null)
{
if ($b) {
return 1;
}
return "";
}
}
class B extends A
{
public static function getB(bool $a): int
{
return self::get($a, true);
}
}',
],
'namespaceFuncNumArgs' => [
'code' => '<?php
namespace Foo;
Expand Down Expand Up @@ -887,6 +912,39 @@ function getSomethingElse()
'ignored_issues' => [],
'php_version' => '7.2',
],
'ineritedreturnTypeBasedOnPhpVersionId' => [
'code' => '<?php
class A {
/**
* @psalm-return (PHP_VERSION_ID is int<70300, max> ? string : int)
*/
function getSomething()
{
return mt_rand(1, 10) > 5 ? "a value" : 42;
}
/**
* @psalm-return (PHP_VERSION_ID is int<70100, max> ? string : int)
*/
function getSomethingElse()
{
return mt_rand(1, 10) > 5 ? "a value" : 42;
}
}
class B extends A {}
$class = new B();
$something = $class->getSomething();
$somethingElse = $class->getSomethingElse();
',
'assertions' => [
'$something' => 'int',
'$somethingElse' => 'string',
],
'ignored_issues' => [],
'php_version' => '7.2',
],
'ineritedConditionalTemplatedReturnType' => [
'code' => '<?php
/** @template InstanceType */
Expand Down

0 comments on commit 5f977d6

Please sign in to comment.