Skip to content

Commit 1c53c0e

Browse files
authored
Merge branch 'main' into 2.x-improve-inspector-extension-and-tests
2 parents c258ef5 + 59b9de0 commit 1c53c0e

28 files changed

+807
-35
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
},
4848
"classmap": [
4949
"tests/src/Type/data",
50-
"tests/src/Rules/data"
50+
"tests/src/Rules/data",
51+
"tests/src/Generics/data"
5152
]
5253
},
5354
"extra": {

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ parameters:
1212
- tests/src
1313
excludePaths:
1414
- tests/src/data/*.php
15+
- tests/src/Generics/data/*.php
1516
- tests/src/Type/data/*.php
1617
- tests/src/Rules/data/*.php
1718
- tests/src/DeprecatedScope/data/*.php
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Drupal\Component\Plugin;
4+
5+
interface DependentPluginInterface {
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Drupal\Component\Plugin;
4+
5+
interface DerivativeInspectionInterface {
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Drupal\Component\Plugin;
4+
5+
abstract class PluginBase implements PluginInspectionInterface, DerivativeInspectionInterface {
6+
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Drupal\Core\Field;
4+
5+
use Drupal\Core\Entity\EntityInterface;
6+
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
7+
8+
/**
9+
* @template T of EntityInterface
10+
* @extends FieldItemList<EntityReferenceItem<T>>
11+
* @implements EntityReferenceFieldItemListInterface<T>
12+
*/
13+
class EntityReferenceFieldItemList extends FieldItemList implements EntityReferenceFieldItemListInterface {
14+
15+
}

stubs/Drupal/Core/Field/FieldItemList.stub

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ namespace Drupal\Core\Field;
55
use Drupal\Core\TypedData\Plugin\DataType\ItemList;
66

77
/**
8-
* @template T of \Drupal\Core\Field\FieldItemInterface
8+
* @template T of FieldItemInterface
99
* @extends ItemList<T>
1010
* @implements FieldItemListInterface<T>
1111
*/
1212
class FieldItemList extends ItemList implements FieldItemListInterface {
1313

1414
/**
15-
* @return \Drupal\Core\Field\FieldItemInterface
15+
* @return T
1616
*/
17-
protected function createItem(int $offset = 0, ?mixed $value = NULL): \Drupal\Core\Field\FieldItemInterface {
18-
}
17+
protected function createItem(int $offset = 0, mixed $value = NULL): FieldItemInterface {}
1918

2019
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Drupal\Core\Field;
4+
5+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6+
7+
/**
8+
* @template T of FieldItemListInterface
9+
* @implements FormatterInterface<T>
10+
*/
11+
abstract class FormatterBase extends PluginSettingsBase implements FormatterInterface, ContainerFactoryPluginInterface {
12+
13+
/**
14+
* @param array<T> $entities_items
15+
*/
16+
public function prepareView(array $entities_items): void {}
17+
18+
/**
19+
* @param T $items
20+
* @param string|null $langcode
21+
*
22+
* @return array<int|string, mixed>
23+
*/
24+
public function view(FieldItemListInterface $items, $langcode = NULL) {}
25+
26+
/**
27+
* @param T $items
28+
* @param string $langcode
29+
*
30+
* @return array<int, array<int|string, mixed>>
31+
*/
32+
public function viewElements(FieldItemListInterface $items, $langcode) {}
33+
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Drupal\Core\Field;
4+
5+
/**
6+
* @template T of FieldItemListInterface
7+
*/
8+
interface FormatterInterface extends PluginSettingsInterface {
9+
10+
/**
11+
* @param array<T> $entities_items
12+
*/
13+
public function prepareView(array $entities_items): void;
14+
15+
/**
16+
* @param T $items
17+
* @param string|null $langcode
18+
*
19+
* @return array<int|string, mixed>
20+
*/
21+
public function view(FieldItemListInterface $items, $langcode = NULL);
22+
23+
/**
24+
* @param T $items
25+
* @param string $langcode
26+
*
27+
* @return array<int, array<int|string, mixed>>
28+
*/
29+
public function viewElements(FieldItemListInterface $items, $langcode);
30+
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
4+
5+
use Drupal\Core\Entity\EntityInterface;
6+
use Drupal\Core\Field\EntityReferenceFieldItemList;
7+
use Drupal\Core\Field\FieldItemListInterface;
8+
use Drupal\Core\Field\FormatterBase;
9+
10+
/**
11+
* @template T of EntityInterface
12+
* @extends FormatterBase<EntityReferenceFieldItemList<T>>
13+
*/
14+
abstract class EntityReferenceFormatterBase extends FormatterBase {
15+
16+
/**
17+
* @param array<EntityReferenceFieldItemList<T>> $entities_items
18+
*/
19+
public function prepareView(array $entities_items): void {}
20+
21+
/**
22+
* @param EntityReferenceFieldItemList<T> $items
23+
* @param string|null $langcode
24+
*
25+
* @return array<int|string, mixed>
26+
*/
27+
public function view(FieldItemListInterface $items, $langcode = NULL) {}
28+
29+
/**
30+
* @param EntityReferenceFieldItemList<T> $items
31+
* @param string $langcode
32+
*
33+
* @return array<int, array<int|string, mixed>>
34+
*/
35+
public function viewElements(FieldItemListInterface $items, $langcode) {}
36+
37+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Drupal\Core\Field;
4+
5+
use Drupal\Component\Plugin\DependentPluginInterface;
6+
use Drupal\Core\Plugin\PluginBase;
7+
8+
abstract class PluginSettingsBase extends PluginBase implements PluginSettingsInterface, DependentPluginInterface {
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Drupal\Core\Field;
4+
5+
use Drupal\Component\Plugin\PluginInspectionInterface;
6+
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
7+
8+
interface PluginSettingsInterface extends PluginInspectionInterface, ThirdPartySettingsInterface {
9+
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Drupal\Core\Plugin;
4+
5+
interface ContainerFactoryPluginInterface {
6+
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Drupal\Core\Plugin;
4+
5+
use Drupal\Component\Plugin\PluginBase as ComponentPluginBase;
6+
7+
abstract class PluginBase extends ComponentPluginBase {
8+
9+
}

tests/src/DeprecatedScope/DeprecationHelperScopeTest.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@
55
namespace mglaman\PHPStanDrupal\Tests\DeprecatedScope;
66

77
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase;
8-
use PHPStan\Rules\Deprecations\CallToDeprecatedFunctionRule;
9-
use PHPStan\Rules\Deprecations\DeprecatedScopeHelper;
8+
use PHPStan\Rules\RestrictedUsage\RestrictedFunctionUsageRule;
109
use PHPStan\Rules\Rule;
1110

1211
final class DeprecationHelperScopeTest extends DrupalRuleTestCase {
1312

1413
protected function getRule(): Rule
1514
{
1615
/** @phpstan-ignore phpstanApi.constructor */
17-
return new CallToDeprecatedFunctionRule(
18-
self::createReflectionProvider(),
19-
/** @phpstan-ignore phpstanApi.classConstant */
20-
self::getContainer()->getByType(DeprecatedScopeHelper::class)
21-
);
16+
return self::getContainer()->getByType(RestrictedFunctionUsageRule::class);
2217
}
2318

2419
public function testCustomScope(): void

tests/src/DeprecatedScope/GlobalLegacyScopeTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@
55
namespace mglaman\PHPStanDrupal\Tests\DeprecatedScope;
66

77
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase;
8-
use PHPStan\Rules\Deprecations\CallToDeprecatedFunctionRule;
9-
use PHPStan\Rules\Deprecations\DeprecatedScopeHelper;
8+
use PHPStan\Rules\RestrictedUsage\RestrictedFunctionUsageRule;
109
use PHPStan\Rules\Rule;
1110

1211
final class GlobalLegacyScopeTest extends DrupalRuleTestCase {
1312

1413
protected function getRule(): Rule
1514
{
16-
/** @phpstan-ignore phpstanApi.constructor */
17-
return new CallToDeprecatedFunctionRule(
18-
self::createReflectionProvider(),
19-
/** @phpstan-ignore phpstanApi.classConstant */
20-
self::getContainer()->getByType(DeprecatedScopeHelper::class)
21-
);
15+
return self::getContainer()->getByType(RestrictedFunctionUsageRule::class);
2216
}
2317

2418
public function testCustomScope(): void

tests/src/DeprecatedScope/IgnoreDeprecationsScopeTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@
55
namespace mglaman\PHPStanDrupal\Tests\DeprecatedScope;
66

77
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase;
8-
use PHPStan\Rules\Deprecations\CallToDeprecatedFunctionRule;
9-
use PHPStan\Rules\Deprecations\DeprecatedScopeHelper;
8+
use PHPStan\Rules\RestrictedUsage\RestrictedFunctionUsageRule;
109
use PHPStan\Rules\Rule;
1110
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
1211

1312
final class IgnoreDeprecationsScopeTest extends DrupalRuleTestCase {
1413

1514
protected function getRule(): Rule
1615
{
17-
/** @phpstan-ignore phpstanApi.constructor */
18-
return new CallToDeprecatedFunctionRule(
19-
self::createReflectionProvider(),
20-
/** @phpstan-ignore phpstanApi.classConstant */
21-
self::getContainer()->getByType(DeprecatedScopeHelper::class)
22-
);
16+
return self::getContainer()->getByType(RestrictedFunctionUsageRule::class);
2317
}
2418

2519
public function testCustomScope(): void
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace mglaman\PHPStanDrupal\Tests\Generics;
6+
7+
use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
8+
use PHPStan\Testing\TypeInferenceTestCase;
9+
10+
final class EntityReferenceFieldItemListGenericTest extends TypeInferenceTestCase
11+
{
12+
use AdditionalConfigFilesTrait;
13+
14+
public static function dataFileAsserts(): iterable
15+
{
16+
yield from self::gatherAssertTypes(__DIR__ . '/data/entity-reference-field-item-list.php');
17+
}
18+
19+
/**
20+
* @dataProvider dataFileAsserts
21+
* @param string $assertType
22+
* @param string $file
23+
* @param mixed ...$args
24+
*/
25+
public function testFileAsserts(
26+
string $assertType,
27+
string $file,
28+
...$args
29+
): void
30+
{
31+
$this->assertFileAsserts($assertType, $file, ...$args);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace mglaman\PHPStanDrupal\Tests\Generics;
6+
7+
use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
8+
use PHPStan\Testing\TypeInferenceTestCase;
9+
10+
final class EntityReferenceFormatterBaseGenericTest extends TypeInferenceTestCase
11+
{
12+
use AdditionalConfigFilesTrait;
13+
14+
public static function dataFileAsserts(): iterable
15+
{
16+
yield from self::gatherAssertTypes(__DIR__ . '/data/entity-reference-formatter-base.php');
17+
}
18+
19+
/**
20+
* @dataProvider dataFileAsserts
21+
* @param string $assertType
22+
* @param string $file
23+
* @param mixed ...$args
24+
*/
25+
public function testFileAsserts(
26+
string $assertType,
27+
string $file,
28+
...$args
29+
): void
30+
{
31+
$this->assertFileAsserts($assertType, $file, ...$args);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace mglaman\PHPStanDrupal\Tests\Generics;
6+
7+
use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
8+
use PHPStan\Testing\TypeInferenceTestCase;
9+
10+
final class EntityReferenceItemGenericTest extends TypeInferenceTestCase
11+
{
12+
use AdditionalConfigFilesTrait;
13+
14+
public static function dataFileAsserts(): iterable
15+
{
16+
yield from self::gatherAssertTypes(__DIR__ . '/data/entity-reference-item.php');
17+
}
18+
19+
/**
20+
* @dataProvider dataFileAsserts
21+
* @param string $assertType
22+
* @param string $file
23+
* @param mixed ...$args
24+
*/
25+
public function testFileAsserts(
26+
string $assertType,
27+
string $file,
28+
...$args
29+
): void
30+
{
31+
$this->assertFileAsserts($assertType, $file, ...$args);
32+
}
33+
}

0 commit comments

Comments
 (0)