Skip to content

Support array shape covariance #2655

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 2 commits into from
Sep 30, 2023
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
2 changes: 1 addition & 1 deletion src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
return TrinaryLogic::createNo();
}

$results[] = TrinaryLogic::createMaybe();
$results[] = TrinaryLogic::createYes();
continue;
} elseif ($hasOffset->maybe() && !$this->isOptionalKey($i)) {
$results[] = TrinaryLogic::createMaybe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,13 @@ public function testRuleWithNullsafeVariant(): void
]);
}

public function testBug5655b(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->analyse([__DIR__ . '/data/bug-5655b.php'], []);
}

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

namespace Bug5655b;

use WeakMap;
use stdClass;


function doFoo() {
// Case with list... OK

/** @var list<array{foo: string, bar?: string}> */
$list = [];

$list[] = [
'foo' => 'baz',
];

// Case with map... FAIL

/** @var WeakMap<object, array{foo: string, bar?: string}> */
$map = new WeakMap();

$map[new stdClass()] = [
'foo' => 'foo',
'bar' => 'bar',
];

$map[new stdClass()] = [
'foo' => 'baz',
];
}

Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,16 @@ public function testBug6181(): void

public function testBug2851b(): void
{
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';

$this->checkAlwaysTrueStrictComparison = true;
$this->analyse([__DIR__ . '/data/bug-2851b.php'], []);
$this->analyse([__DIR__ . '/data/bug-2851b.php'], [
[
'Strict comparison using === between 0 and 0 will always evaluate to true.',
21,
$tipText,
],
]);
}

public function testBug8158(): void
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,11 @@ public function testListReturnTypeCovariance(): void
]);
}

public function testBug9905(): void
{
$this->reportMaybes = true;
$this->reportStatic = true;
$this->analyse([__DIR__ . '/data/bug-9905.php'], []);
}

}
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-9905.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Bug9905;

interface Foo {
/** @return array{field: string, extra?: string} */
public function get(): array;
}

class AlwaysExtra implements Foo {
/** @return array{field: string, extra: string} */
public function get(): array {
return ['field' => 'user', 'extra' => 'readonly'];
}
}

class NeverExtra implements Foo {
/** @return array{field: string} */
public function get(): array {
return ['field' => 'user'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,19 @@ public function testBug7352WithSubNamespace(): void
$this->analyse([__DIR__ . '/data/bug-7352-with-sub-namespace.php'], []);
}

public function testBug7273(): void
{
$this->analyse([__DIR__ . '/data/bug-7273.php'], []);
}

public function testBug7273b(): void
{
$this->analyse([__DIR__ . '/data/bug-7273b.php'], []);
}

public function testBug5655(): void
{
$this->analyse([__DIR__ . '/data/bug-5655.php'], []);
}

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

namespace Bug5655;

abstract class Error
{

/**
* @var array{format: string, specificity?: int}
*/
const SPEC = [
'format' => '',
];
}
75 changes: 75 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/bug-7273.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php declare(strict_types=1);

namespace Bug7273;

/**
* @template I
* @template O
*/
interface ConfigValueProcessorInterface
{
/**
* @param I $value
*
* @return O
*/
public function process(mixed $value, mixed $options): mixed;

/**
* @param O $value
*
* @return I
*/
public function unprocess(mixed $value, mixed $options): mixed;
}

/**
* @implements ConfigValueProcessorInterface<string, int>
*/
abstract class SomeValueProcessor implements ConfigValueProcessorInterface
{
}

interface ConfigKey
{
public const ACTIVITY__EXPORT__TYPE = 'activity.export.type';
public const ACTIVITY__TAGS__MULTI = 'activity.tags.multi';
// ...
}

interface Module
{
public const ABSENCEREQUEST = 'absencerequest';
public const ACTIVITY = 'activity';
// ...
}

interface ConfigRepositoryInterface
{
/**
* @var array<ConfigKey::*, array{
* type?: non-empty-string,
* default: mixed,
* acl: non-empty-string[],
* linked_module?: Module::*,
* value_processors?: array<class-string<ConfigValueProcessorInterface<mixed, mixed>>, mixed>,
* }>
*/
public const CONFIGURATIONS = [
ConfigKey::ACTIVITY__EXPORT__TYPE => [
'type' => "'SomeExport'|'SomeOtherExport'|null",
'default' => null,
'acl' => ['superadmin'],
'linked_module' => Module::ACTIVITY,
],
ConfigKey::ACTIVITY__TAGS__MULTI => [
'default' => false,
'acl' => ['admin'],
'linked_module' => Module::ACTIVITY,
'value_processors' => [
SomeValueProcessor::class => ['someOption' => true],
],
],
// ...
];
}
51 changes: 51 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/bug-7273b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

namespace Bug7273b;

class HelloWorld
{
/** @var array<non-empty-string, array{url?: non-empty-string, title: non-empty-string}> */
public const FIRST_EXAMPLE = [
'image.png' => [
'url' => '/first-link',
'title' => 'First Link',
],
'directory/image.jpg' => [
'url' => '/second-link',
'title' => 'Second Link',
],
'another-image.jpg' => [
'title' => 'An Image',
],
];

/** @var array<string, array{url?: string, title: string}> */
public const SECOND_EXAMPLE = [
'image.png' => [
'url' => '/first-link',
'title' => 'First Link',
],
'directory/image.jpg' => [
'url' => '/second-link',
'title' => 'Second Link',
],
'another-image.jpg' => [
'title' => 'An Image',
],
];

/** @var array{url?: string, title: string}[] */
public const THIRD_EXAMPLE = [
'image.png' => [
'url' => '/first-link',
'title' => 'First Link',
],
'directory/image.jpg' => [
'url' => '/second-link',
'title' => 'Second Link',
],
'another-image.jpg' => [
'title' => 'An Image',
],
];
}
64 changes: 64 additions & 0 deletions tests/PHPStan/Type/Constant/ConstantArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,73 @@ public function dataIsSuperTypeOf(): iterable
new IntegerType(),
], 2, [0, 1]),
new ConstantArrayType([], []),
TrinaryLogic::createYes(),
];

yield [
new ConstantArrayType([
new ConstantStringType('foo'),
new ConstantStringType('bar'),
], [
new IntegerType(),
new IntegerType(),
], 2, [0, 1]),
new ConstantArrayType([
new ConstantStringType('foo'),
], [
new IntegerType(),
], 1, [0]),
TrinaryLogic::createYes(),
];

yield [
new ConstantArrayType([
new ConstantStringType('foo'),
], [
new IntegerType(),
]),
new ConstantArrayType([
new ConstantStringType('foo'),
new ConstantStringType('bar'),
], [
new IntegerType(),
new IntegerType(),
], 2, [0, 1]),
TrinaryLogic::createMaybe(),
];

yield [
new ConstantArrayType([
new ConstantStringType('foo'),
], [
new StringType(),
]),
new ConstantArrayType([
new ConstantStringType('foo'),
new ConstantStringType('bar'),
], [
new IntegerType(),
new IntegerType(),
], 2, [0, 1]),
TrinaryLogic::createNo(),
];

yield [
new ConstantArrayType([
new ConstantStringType('foo'),
new ConstantStringType('bar'),
], [
new IntegerType(),
new IntegerType(),
], 2, [0, 1]),
new ConstantArrayType([
new ConstantStringType('foo'),
], [
new StringType(),
]),
TrinaryLogic::createNo(),
];

yield [
new ConstantArrayType([], []),
new ConstantArrayType([
Expand Down