Skip to content

Commit 407b652

Browse files
committed
Add some more utility methods.
1 parent 6c41190 commit 407b652

File tree

3 files changed

+122
-13
lines changed

3 files changed

+122
-13
lines changed

src/Doc.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,36 @@ public function getTags(): Tags {
4343
return $this->tags;
4444
}
4545

46+
/**
47+
* @return array<int, Tag>
48+
* @phpstan-return list<Tag>
49+
*/
50+
public function getTagsByType( string $type ): array {
51+
return $this->tags->getByType( $type );
52+
}
53+
4654
/**
4755
* @return array<int, Tag>
4856
* @phpstan-return list<Tag>
4957
*/
5058
public function getParams(): array {
51-
return $this->getTags()->getParams();
59+
return $this->tags->getParams();
60+
}
61+
62+
public function getReturnTypeString(): ?string {
63+
return $this->tags->getReturnTypeString();
64+
}
65+
66+
/**
67+
* @return ?array<int, string>
68+
* @phpstan-return ?list<string>
69+
*/
70+
public function getReturnTypes(): ?array {
71+
return $this->tags->getReturnTypes();
72+
}
73+
74+
public function countParams(): int {
75+
return $this->tags->countParams();
5276
}
5377

5478
/**

src/Tags.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function count(): int {
2828
return count( $this->tags );
2929
}
3030

31+
public function countParams(): int {
32+
return count( $this->getParams() );
33+
}
34+
3135
/**
3236
* @return \Traversable<int, Tag>
3337
*/
@@ -48,15 +52,47 @@ public function all(): array {
4852
* @phpstan-return list<Tag>
4953
*/
5054
public function getParams(): array {
51-
$params = [];
55+
return $this->getByType( 'param' );
56+
}
57+
58+
/**
59+
* @return array<int, Tag>
60+
* @phpstan-return list<Tag>
61+
*/
62+
public function getByType( string $type ): array {
63+
$tags = [];
64+
65+
foreach ( $this->tags as $tag ) {
66+
if ( $tag->getName() === $type ) {
67+
$tags[] = $tag;
68+
}
69+
}
70+
71+
return $tags;
72+
}
5273

53-
foreach ( $this as $tag ) {
74+
/**
75+
* @return ?array<int, string>
76+
* @phpstan-return ?list<string>
77+
*/
78+
public function getReturnTypes(): ?array {
79+
foreach ( $this->tags as $tag ) {
5480
if ( $tag->getName() === 'param' ) {
55-
$params[] = $tag;
81+
return $tag->getTypes();
5682
}
5783
}
5884

59-
return $params;
85+
return null;
86+
}
87+
88+
public function getReturnTypeString(): ?string {
89+
$returnTypes = $this->getReturnTypes();
90+
91+
if ( $returnTypes === null ) {
92+
return null;
93+
}
94+
95+
return implode( '|', $returnTypes );
6096
}
6197

6298
/**

tests/phpunit/HooksTest.php

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPUnit\Framework\TestCase;
77
use WPHooks\Hook;
88
use WPHooks\Hooks;
9+
use WPHooks\Tag;
910

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

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

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

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

4948
self::assertFalse( $includes );
5049
self::assertNull( $hook );
5150
}
5251

52+
public function testCanGetReturnTypes(): void {
53+
$hooks = $this->getFilters();
54+
$hook = $hooks->find( 'wp_tag_cloud' );
55+
56+
$returnTypes = $hook->getDoc()->getReturnTypes();
57+
$expected = [
58+
'string',
59+
'string[]',
60+
];
61+
62+
self::assertSame( $expected, $returnTypes );
63+
}
64+
65+
public function testCanGetReturnTypeString(): void {
66+
$hooks = $this->getFilters();
67+
$hook = $hooks->find( 'wp_tag_cloud' );
68+
69+
$returnType = $hook->getDoc()->getReturnTypeString();
70+
71+
self::assertSame( 'string|string[]', $returnType );
72+
}
73+
74+
public function testCanGetParams(): void {
75+
$hooks = $this->getFilters();
76+
$hook = $hooks->find( 'wp_tag_cloud' );
77+
78+
$params = $hook->getDoc()->getParams();
79+
80+
self::assertCount( 2, $params );
81+
self::assertInstanceOf( Tag::class, $params[0] );
82+
self::assertInstanceOf( Tag::class, $params[1] );
83+
}
84+
85+
public function testCanCountParams(): void {
86+
$hooks = $this->getFilters();
87+
$hook = $hooks->find( 'wp_tag_cloud' );
88+
89+
$count = $hook->getDoc()->countParams();
90+
91+
self::assertSame( 2, $count );
92+
}
93+
5394
/**
5495
* @return array<string, array<int, string>>
5596
* @phpstan-return array{
@@ -93,4 +134,12 @@ public function dataCoreFiles(): array {
93134
],
94135
];
95136
}
137+
138+
private function getFilters(): Hooks {
139+
return Hooks::fromFile( $this->dataCoreFiles()['filters'][0] );
140+
}
141+
142+
private function getActions(): Hooks {
143+
return Hooks::fromFile( $this->dataCoreFiles()['actions'][0] );
144+
}
96145
}

0 commit comments

Comments
 (0)