Skip to content

Commit 91491a0

Browse files
authored
Replace AccessibleReturnTypeExtension with stub for AccessibleInterface (#419)
* Stub conditional return on AccessibleInterface::access * use stub over AccessibleReturnTypeExtension
1 parent 28f06af commit 91491a0

File tree

5 files changed

+52
-46
lines changed

5 files changed

+52
-46
lines changed

extension.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,6 @@ services:
276276
-
277277
class: mglaman\PHPStanDrupal\Type\UrlToStringDynamicReturnTypeExtension
278278
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
279-
-
280-
class: mglaman\PHPStanDrupal\Type\AccessibleReturnTypeExtension
281-
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
282279
-
283280
class: mglaman\PHPStanDrupal\Type\EntityAccessControlHandlerReturnTypeExtension
284281
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

src/Type/AccessibleReturnTypeExtension.php

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Drupal\Core\Access;
4+
5+
use Drupal\Core\Session\AccountInterface;
6+
7+
interface AccessibleInterface {
8+
9+
/**
10+
* @param string $operation
11+
* @param \Drupal\Core\Session\AccountInterface $account
12+
* @param bool $return_as_object
13+
*
14+
* @return ($return_as_object is true ? \Drupal\Core\Access\AccessResultInterface : bool)
15+
*/
16+
public function access(string $operation, AccountInterface $account = NULL, bool $return_as_object = FALSE);
17+
}

tests/src/Type/AccessibleReturnTypeExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ final class AccessibleReturnTypeExtensionTest extends TypeInferenceTestCase {
1212
public function dataFileAsserts(): iterable
1313
{
1414
yield from $this->gatherAssertTypes(__DIR__ . '/data/accessible.php');
15+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-414.php');
1516
}
1617

1718
/**

tests/src/Type/data/bug-414.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Bug414;
4+
5+
use Drupal\Core\Access\AccessResultInterface;
6+
use Drupal\Core\Entity\EntityInterface;
7+
use function PHPStan\Testing\assertType;
8+
9+
function (EntityInterface $entity): void {
10+
assertType('bool', $entity->access('view'));
11+
assertType(AccessResultInterface::class, $entity->access('view', null, true));
12+
};
13+
14+
function (EntityInterface $entity): void {
15+
$accessArgs = ['delete'];
16+
$access = $entity->access(...$accessArgs);
17+
assertType('bool', $access);
18+
19+
$accessArgs = ['delete', null, false];
20+
$access = $entity->access(...$accessArgs);
21+
assertType('bool', $access);
22+
23+
// @todo test after https://github.com/phpstan/phpstan/issues/7369
24+
// $accessArgs = ['delete', null, true];
25+
// $access = $entity->access(...$accessArgs);
26+
// assertType(AccessResultInterface::class, $access);
27+
};
28+
29+
function (EntityInterface $entity): void {
30+
$access = $entity->access('delete', return_as_object: true);
31+
assertType(AccessResultInterface::class, $access);
32+
$access = $entity->access('delete', return_as_object: false);
33+
assertType('bool', $access);
34+
};

0 commit comments

Comments
 (0)