Skip to content

Commit

Permalink
Add some more utility methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbillion committed Sep 8, 2024
1 parent 6c41190 commit 407b652
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 13 deletions.
26 changes: 25 additions & 1 deletion src/Doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,36 @@ public function getTags(): Tags {
return $this->tags;
}

/**
* @return array<int, Tag>
* @phpstan-return list<Tag>
*/
public function getTagsByType( string $type ): array {
return $this->tags->getByType( $type );
}

/**
* @return array<int, Tag>
* @phpstan-return list<Tag>
*/
public function getParams(): array {
return $this->getTags()->getParams();
return $this->tags->getParams();
}

public function getReturnTypeString(): ?string {
return $this->tags->getReturnTypeString();
}

/**
* @return ?array<int, string>
* @phpstan-return ?list<string>
*/
public function getReturnTypes(): ?array {
return $this->tags->getReturnTypes();
}

public function countParams(): int {
return $this->tags->countParams();
}

/**
Expand Down
44 changes: 40 additions & 4 deletions src/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function count(): int {
return count( $this->tags );
}

public function countParams(): int {
return count( $this->getParams() );
}

/**
* @return \Traversable<int, Tag>
*/
Expand All @@ -48,15 +52,47 @@ public function all(): array {
* @phpstan-return list<Tag>
*/
public function getParams(): array {
$params = [];
return $this->getByType( 'param' );
}

/**
* @return array<int, Tag>
* @phpstan-return list<Tag>
*/
public function getByType( string $type ): array {
$tags = [];

foreach ( $this->tags as $tag ) {
if ( $tag->getName() === $type ) {
$tags[] = $tag;
}
}

return $tags;
}

foreach ( $this as $tag ) {
/**
* @return ?array<int, string>
* @phpstan-return ?list<string>
*/
public function getReturnTypes(): ?array {
foreach ( $this->tags as $tag ) {
if ( $tag->getName() === 'param' ) {
$params[] = $tag;
return $tag->getTypes();
}
}

return $params;
return null;
}

public function getReturnTypeString(): ?string {
$returnTypes = $this->getReturnTypes();

if ( $returnTypes === null ) {
return null;
}

return implode( '|', $returnTypes );
}

/**
Expand Down
65 changes: 57 additions & 8 deletions tests/phpunit/HooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPUnit\Framework\TestCase;
use WPHooks\Hook;
use WPHooks\Hooks;
use WPHooks\Tag;

final class HooksTest extends TestCase {
/**
Expand All @@ -30,26 +31,66 @@ public function testErrorThrownWhenFileDoesNotExist(): void {
}

public function testCanFindByName(): void {
$file = $this->dataCoreFiles()['filters'][0];
$filters = Hooks::fromFile( $file );
$hook = $filters->find( 'wp_tag_cloud' );
$includes = $filters->includes( 'wp_tag_cloud' );
$hooks = $this->getFilters();
$hook = $hooks->find( 'wp_tag_cloud' );
$includes = $hooks->includes( 'wp_tag_cloud' );

self::assertTrue( $includes );
self::assertInstanceOf( Hook::class, $hook );
self::assertSame( 'wp_tag_cloud', $hook->getName() );
}

public function testFindByUnknownNameReturnsNull(): void {
$file = $this->dataCoreFiles()['filters'][0];
$filters = Hooks::fromFile( $file );
$hook = $filters->find( 'this_does_not_exist' );
$includes = $filters->includes( 'this_does_not_exist' );
$hooks = $this->getFilters();
$hook = $hooks->find( 'this_does_not_exist' );
$includes = $hooks->includes( 'this_does_not_exist' );

self::assertFalse( $includes );
self::assertNull( $hook );
}

public function testCanGetReturnTypes(): void {
$hooks = $this->getFilters();
$hook = $hooks->find( 'wp_tag_cloud' );

$returnTypes = $hook->getDoc()->getReturnTypes();
$expected = [
'string',
'string[]',
];

self::assertSame( $expected, $returnTypes );
}

public function testCanGetReturnTypeString(): void {
$hooks = $this->getFilters();
$hook = $hooks->find( 'wp_tag_cloud' );

$returnType = $hook->getDoc()->getReturnTypeString();

self::assertSame( 'string|string[]', $returnType );
}

public function testCanGetParams(): void {
$hooks = $this->getFilters();
$hook = $hooks->find( 'wp_tag_cloud' );

$params = $hook->getDoc()->getParams();

self::assertCount( 2, $params );
self::assertInstanceOf( Tag::class, $params[0] );
self::assertInstanceOf( Tag::class, $params[1] );
}

public function testCanCountParams(): void {
$hooks = $this->getFilters();
$hook = $hooks->find( 'wp_tag_cloud' );

$count = $hook->getDoc()->countParams();

self::assertSame( 2, $count );
}

/**
* @return array<string, array<int, string>>
* @phpstan-return array{
Expand Down Expand Up @@ -93,4 +134,12 @@ public function dataCoreFiles(): array {
],
];
}

private function getFilters(): Hooks {
return Hooks::fromFile( $this->dataCoreFiles()['filters'][0] );
}

private function getActions(): Hooks {
return Hooks::fromFile( $this->dataCoreFiles()['actions'][0] );
}
}

0 comments on commit 407b652

Please sign in to comment.