Skip to content
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
58 changes: 58 additions & 0 deletions types/Macros/Arr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/
use Hyperf\Collection\Arr;

use function PHPStan\Testing\assertType;

// Arr::arrayable() tests
assertType('bool', Arr::arrayable([]));
assertType('bool', Arr::arrayable(['key' => 'value']));
assertType('bool', Arr::arrayable('string'));

// Arr::array() tests
assertType('array', Arr::array(['nested' => ['key' => 'value']], 'nested'));
assertType('array', Arr::array(['data' => [1, 2, 3]], 'data', []));

// Arr::boolean() tests
assertType('bool', Arr::boolean(['is_active' => true], 'is_active'));
assertType('bool', Arr::boolean(['flag' => false], 'flag', false));

// Arr::every() tests
assertType('bool', Arr::every([1, 2, 3], fn ($value) => $value > 0));
assertType('bool', Arr::every(['a', 'b', 'c'], fn ($value) => is_string($value)));

// Arr::float() tests
assertType('float', Arr::float(['price' => 10.5], 'price'));
assertType('float', Arr::float(['amount' => 99.99], 'amount', 0.0));

// Arr::from() tests
assertType('array', Arr::from([]));
assertType('array', Arr::from(['key' => 'value']));

// Arr::hasAll() tests
assertType('bool', Arr::hasAll(['a' => 1, 'b' => 2], ['a', 'b']));
assertType('bool', Arr::hasAll(['x' => 1], 'x'));

// Arr::integer() tests
assertType('int', Arr::integer(['count' => 10], 'count'));
assertType('int', Arr::integer(['total' => 100], 'total', 0));

// Arr::string() tests
assertType('string', Arr::string(['name' => 'test'], 'name'));
assertType('string', Arr::string(['title' => 'value'], 'title', 'default'));

// Arr::some() tests
assertType('bool', Arr::some([1, 2, 3], fn ($value) => $value > 2));
assertType('bool', Arr::some(['a', 'b', 'c'], fn ($value) => $value === 'b'));

// Arr::sortByMany() tests
assertType('array', Arr::sortByMany([['name' => 'John', 'age' => 30]], [['name', true]]));
assertType('array', Arr::sortByMany([['x' => 1], ['x' => 2]], []));
22 changes: 22 additions & 0 deletions types/Macros/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/
use Hyperf\Collection\Collection;

use function PHPStan\Testing\assertType;

// Collection::isSingle() tests
assertType('bool', (new Collection([1]))->isSingle());
assertType('bool', (new Collection([1, 2]))->isSingle());
assertType('bool', (new Collection([]))->isSingle());

// Collection::collapseWithKeys() tests
assertType('Hyperf\Collection\Collection', (new Collection([['a' => 1], ['b' => 2]]))->collapseWithKeys());
assertType('Hyperf\Collection\Collection', (new Collection([]))->collapseWithKeys());
22 changes: 22 additions & 0 deletions types/Macros/LazyCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/
use Hyperf\Collection\LazyCollection;

use function PHPStan\Testing\assertType;

// LazyCollection::isSingle() tests
assertType('bool', LazyCollection::make([1])->isSingle());
assertType('bool', LazyCollection::make([1, 2])->isSingle());
assertType('bool', LazyCollection::make([])->isSingle());

// LazyCollection::collapseWithKeys() tests
assertType('Hyperf\Collection\LazyCollection', LazyCollection::make([['a' => 1], ['b' => 2]])->collapseWithKeys());
assertType('Hyperf\Collection\LazyCollection', LazyCollection::make([])->collapseWithKeys());
109 changes: 109 additions & 0 deletions types/Macros/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/
use Hyperf\Context\Context;
use Hyperf\HttpServer\Request;

use function PHPStan\Testing\assertType;

// Create a mock request for testing
Context::set(\Psr\Http\Message\ServerRequestInterface::class, new \Hyperf\HttpMessage\Server\Request('GET', '/'));
$request = new Request();

// Request::boolean() tests
assertType('bool', $request->boolean('is_active'));
assertType('bool', $request->boolean('flag', false));

// Request::collect() tests
assertType('mixed', $request->collect());
assertType('Hyperf\Collection\Collection', $request->collect('key'));
assertType('Hyperf\Collection\Collection', $request->collect(['key1', 'key2']));

// Request::date() tests
assertType('Carbon\Carbon|null', $request->date('created_at'));
assertType('Carbon\Carbon|null', $request->date('updated_at', 'Y-m-d'));
assertType('Carbon\Carbon|null', $request->date('deleted_at', 'Y-m-d H:i:s', 'UTC'));

// Request::enum() tests
assertType('mixed', $request->enum('status', \BackedEnum::class));

// Request::exists() tests
assertType('bool', $request->exists('key'));

// Request::filled() tests
assertType('bool', $request->filled('name'));
assertType('bool', $request->filled(['name', 'email']));

// Request::float() tests
assertType('float', $request->float('price'));
assertType('float', $request->float('amount', 0.0));

// Request::fluent() tests
assertType('Hyperf\Support\Fluent', $request->fluent());
assertType('Hyperf\Support\Fluent', $request->fluent('key'));
assertType('Hyperf\Support\Fluent', $request->fluent(['key1', 'key2']));

// Request::hasAny() tests
assertType('bool', $request->hasAny(['key1', 'key2']));
assertType('bool', $request->hasAny('key'));

// Request::host() tests
assertType('string', $request->host());

// Request::httpHost() tests
assertType('string', $request->httpHost());

// Request::integer() tests
assertType('int', $request->integer('count'));
assertType('int', $request->integer('total', 0));

// Request::isEmptyString() tests
assertType('bool', $request->isEmptyString('field'));

// Request::isJson() tests
assertType('bool', $request->isJson());

// Request::isNotFilled() tests
assertType('bool', $request->isNotFilled('field'));
assertType('bool', $request->isNotFilled(['field1', 'field2']));

// Request::keys() tests
assertType('array', $request->keys());

// Request::missing() tests
assertType('bool', $request->missing('key'));
assertType('bool', $request->missing(['key1', 'key2']));

// Request::str() tests
assertType('Hyperf\Stringable\Stringable', $request->str('name'));
assertType('Hyperf\Stringable\Stringable', $request->str('title', 'default'));

// Request::string() tests
assertType('Hyperf\Stringable\Stringable', $request->string('name'));
assertType('Hyperf\Stringable\Stringable', $request->string('title', 'default'));

// Request::wantsJson() tests
assertType('bool', $request->wantsJson());

// Request::isSecure() tests
assertType('bool', $request->isSecure());

// Request::getScheme() tests
assertType('string', $request->getScheme());

// Request::getPort() tests
assertType('int', $request->getPort());

// Request::schemeAndHttpHost() tests
assertType('string', $request->schemeAndHttpHost());

// Request::anyFilled() tests
assertType('bool', $request->anyFilled(['key1', 'key2']));
assertType('bool', $request->anyFilled('key'));
44 changes: 44 additions & 0 deletions types/Macros/Str.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/
use Hyperf\Stringable\Str;

use function PHPStan\Testing\assertType;

// Str::createUuidsNormally() tests
assertType('void', Str::createUuidsNormally());

// Str::createUuidsUsing() tests
assertType('void', Str::createUuidsUsing(null));
assertType('void', Str::createUuidsUsing(fn () => 'custom-uuid'));

// Str::deduplicate() tests
assertType('string', Str::deduplicate('hello world'));
assertType('string', Str::deduplicate('a--b--c', '-'));

// Str::inlineMarkdown() tests
assertType('string', Str::inlineMarkdown('**bold**'));
assertType('string', Str::inlineMarkdown('*italic*', []));

// Str::markdown() tests
assertType('string', Str::markdown('# Heading'));
assertType('string', Str::markdown('## Title', [], []));

// Str::transliterate() tests
assertType('string', Str::transliterate('こんにちは'));
assertType('string', Str::transliterate('café', '?', false));

// Str::doesntEndWith() tests
assertType('bool', Str::doesntEndWith('hello world', 'world'));
assertType('bool', Str::doesntEndWith('test', ['ing', 'ed']));

// Str::doesntStartWith() tests
assertType('bool', Str::doesntStartWith('hello world', 'hello'));
assertType('bool', Str::doesntStartWith('test', ['pre', 'post']));
44 changes: 44 additions & 0 deletions types/Macros/Stringable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/
use Hyperf\Stringable\Str;

use function PHPStan\Testing\assertType;

// Stringable::deduplicate() tests
assertType('Hyperf\Stringable\Stringable', Str::of('hello world')->deduplicate());
assertType('Hyperf\Stringable\Stringable', Str::of('a--b')->deduplicate('-'));

// Stringable::hash() tests
assertType('Hyperf\Stringable\Stringable', Str::of('password')->hash('sha256'));
assertType('Hyperf\Stringable\Stringable', Str::of('text')->hash('md5'));

// Stringable::inlineMarkdown() tests
assertType('Hyperf\Stringable\Stringable', Str::of('**bold**')->inlineMarkdown());
assertType('Hyperf\Stringable\Stringable', Str::of('*italic*')->inlineMarkdown([]));

// Stringable::markdown() tests
assertType('Hyperf\Stringable\Stringable', Str::of('# Heading')->markdown());
assertType('Hyperf\Stringable\Stringable', Str::of('## Title')->markdown([], []));

// Stringable::toHtmlString() tests
assertType('FriendsOfHyperf\Support\HtmlString', Str::of('<p>test</p>')->toHtmlString());

// Stringable::whenIsAscii() tests
assertType('Hyperf\Stringable\Stringable', Str::of('hello')->whenIsAscii(fn ($s) => $s->upper()));
assertType('Hyperf\Stringable\Stringable', Str::of('test')->whenIsAscii(fn ($s) => $s->upper(), fn ($s) => $s->lower()));

// Stringable::doesntEndWith() tests
assertType('bool', Str::of('hello world')->doesntEndWith('world'));
assertType('bool', Str::of('test')->doesntEndWith(['ing', 'ed']));

// Stringable::doesntStartWith() tests
assertType('bool', Str::of('hello world')->doesntStartWith('hello'));
assertType('bool', Str::of('test')->doesntStartWith(['pre', 'post']));