Skip to content

Commit 24fa3e9

Browse files
authored
Mark LockBackendInterface::acquire() as impure (#736)
* test lock backend impure acquire * add stub * fix assertions
1 parent 4d60ae9 commit 24fa3e9

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Drupal\Core\Lock;
4+
5+
interface LockBackendInterface {
6+
7+
/**
8+
* @param string $name
9+
* @param float $timeout
10+
*
11+
* @return bool
12+
*
13+
* @phpstan-impure
14+
*/
15+
public function acquire(string $name, float $timeout = 30.0): bool;
16+
17+
18+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace mglaman\PHPStanDrupal\Tests\Type;
4+
5+
use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
6+
use PHPStan\Testing\TypeInferenceTestCase;
7+
8+
final class LockBackendInterfaceStubTest extends TypeInferenceTestCase
9+
{
10+
use AdditionalConfigFilesTrait;
11+
12+
public function dataFileAsserts(): iterable
13+
{
14+
yield from self::gatherAssertTypes(__DIR__ . '/data/lock-backend.php');
15+
}
16+
17+
/**
18+
* @dataProvider dataFileAsserts
19+
* @param string $assertType
20+
* @param string $file
21+
* @param mixed ...$args
22+
*/
23+
public function testFileAsserts(
24+
string $assertType,
25+
string $file,
26+
...$args
27+
): void
28+
{
29+
$this->assertFileAsserts($assertType, $file, ...$args);
30+
}
31+
}

tests/src/Type/data/lock-backend.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Foo;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
$lock = \Drupal::lock();
8+
9+
if (!$lock->acquire('foo')) {
10+
assertType('bool', $lock->acquire('foo'));
11+
if ($lock->acquire('foo')) {
12+
assertType('bool', $lock->acquire('foo'));
13+
}
14+
}

0 commit comments

Comments
 (0)