From 407caf521186ec1f03024f39031cc681ad491026 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 23 Jan 2023 19:46:53 +0000 Subject: [PATCH] Updated for pthreads 5.0 --- .github/workflows/main.yml | 3 ++- composer.json | 2 +- src/BaseClassLoader.php | 37 +++++++++++++++++++------------ tests/phpstan/stubs/pthreads.stub | 26 ++++++++++++++++++---- 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c7818f8..fb63739 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -18,10 +18,11 @@ jobs: - uses: actions/checkout@v3 - name: Setup PHP - uses: pmmp/setup-php-action@87cc7c2f65e10e0e64be54a3e41a62a4c61bd1a9 + uses: pmmp/setup-php-action@6dd74c145250109942b08fc63cd5118edd2fd256 with: php-version: ${{ matrix.php }} install-path: "./bin" + pm-version-major: "5" - name: Cache Composer packages id: composer-cache diff --git a/composer.json b/composer.json index 25c25ae..dd32070 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "LGPL-3.0", "require": { "php": "^8.0", - "ext-pthreads": "~3.2.0 || ^4.0", + "ext-pthreads": "^5.0", "ext-reflection": "*" }, "require-dev": { diff --git a/src/BaseClassLoader.php b/src/BaseClassLoader.php index 6131c21..dd68f4d 100644 --- a/src/BaseClassLoader.php +++ b/src/BaseClassLoader.php @@ -17,16 +17,22 @@ declare(strict_types=1); -class BaseClassLoader extends \Threaded implements DynamicClassLoader{ +class BaseClassLoader extends \ThreadedBase implements DynamicClassLoader{ - /** @var \Threaded|string[] */ + /** + * @var \ThreadedArray|string[] + * @phpstan-var \ThreadedArray + */ private $fallbackLookup; - /** @var \Threaded|string[][] */ + /** + * @var \ThreadedArray|string[][] + * @phpstan-var \ThreadedArray> + */ private $psr4Lookup; public function __construct(){ - $this->fallbackLookup = new \Threaded; - $this->psr4Lookup = new \Threaded; + $this->fallbackLookup = new \ThreadedArray(); + $this->psr4Lookup = new \ThreadedArray(); } protected function normalizePath(string $path) : string{ @@ -46,17 +52,19 @@ public function addPath(string $namespacePrefix, string $path, bool $prepend = f }else{ $namespacePrefix = trim($namespacePrefix, '\\') . '\\'; $this->psr4Lookup->synchronized(function() use ($namespacePrefix, $path, $prepend) : void{ - /** @var \Threaded|null $list */ $list = $this->psr4Lookup[$namespacePrefix] ?? null; if($list === null){ - $list = $this->psr4Lookup[$namespacePrefix] = new \Threaded; + $list = $this->psr4Lookup[$namespacePrefix] = new \ThreadedArray(); } $this->appendOrPrependLookupEntry($list, $path, $prepend); }); } } - protected function appendOrPrependLookupEntry(\Threaded $list, string $entry, bool $prepend) : void{ + /** + * @phpstan-param \ThreadedArray $list + */ + protected function appendOrPrependLookupEntry(\ThreadedArray $list, string $entry, bool $prepend) : void{ if($prepend){ $entries = $this->getAndRemoveLookupEntries($list); $list[] = $entry; @@ -69,12 +77,15 @@ protected function appendOrPrependLookupEntry(\Threaded $list, string $entry, bo } /** - * @return mixed[] + * @return string[] + * + * @phpstan-param \ThreadedArray $list + * @phpstan-return list */ - protected function getAndRemoveLookupEntries(\Threaded $list) : array{ + protected function getAndRemoveLookupEntries(\ThreadedArray $list) : array{ $entries = []; - while($list->count() > 0){ - $entries[] = $list->shift(); + while(($entry = $list->shift()) !== null){ + $entries[] = $entry; } return $entries; } @@ -127,8 +138,6 @@ public function findClass(string $name) : ?string{ while(false !== $lastPos = strrpos($subPath, '\\')){ $subPath = substr($subPath, 0, $lastPos); $search = $subPath . '\\'; - - /** @var \Threaded|null $lookup */ $lookup = $this->psr4Lookup[$search] ?? null; if($lookup !== null){ $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); diff --git a/tests/phpstan/stubs/pthreads.stub b/tests/phpstan/stubs/pthreads.stub index 10a558a..a48fcee 100644 --- a/tests/phpstan/stubs/pthreads.stub +++ b/tests/phpstan/stubs/pthreads.stub @@ -1,15 +1,33 @@ + * @implements \IteratorAggregate */ -class Threaded implements \Traversable{ +abstract class ThreadedBase implements \IteratorAggregate{ /** * @template TReturn - * @param callable() : TReturn $function + * @param \Closure() : TReturn $function * @param mixed ...$args * @return TReturn */ - public function synchronized(callable $function, ...$args){} + public function synchronized(\Closure $function, mixed ...$args) : mixed{} } + +/** + * @template TKey of array-key + * @template TValue + * @implements ArrayAccess + */ +final class ThreadedArray extends ThreadedBase implements Countable, ArrayAccess{ + + /** + * @return TValue|null + */ + public function pop() : mixed{} + + /** + * @return TValue|null + */ + public function shift() : mixed{} +} \ No newline at end of file