Skip to content

Commit 9280e52

Browse files
authored
Merge pull request #45 from stellarwp/bugfix/proper-phpstan-annotations
Assets::get() phpstan fix and add Assets::remove_all() method
2 parents 696c873 + 5053c0b commit 9280e52

File tree

2 files changed

+123
-24
lines changed

2 files changed

+123
-24
lines changed

src/Assets/Assets.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,19 @@ public function filter_print_before_after_script( $tag, $handle ): string {
190190
/**
191191
* Get the Asset Object configuration.
192192
*
193-
* @param string|array $slug Slug of the Asset.
194-
* @param boolean $sort If we should do any sorting before returning.
193+
* @param string|string[]|null $slug Slug of the Asset, array of slugs, or null for all assets.
194+
* @param bool $sort If we should do any sorting before returning.
195195
*
196-
* @return array|Asset Array of asset objects, single asset object, or null if looking for a single asset but
197-
* it was not in the array of objects.
198-
* @since 1.0.0
196+
* @return Asset[]|Asset|null Array of asset objects, a single Asset object, or null if looking
197+
* for a single Asset but it was not found.
198+
*
199+
* @phpstan-return (
200+
* $slug is null ? array<string, Asset> :
201+
* ($slug is string ? Asset|null :
202+
* ($slug is array ? array<string, Asset> : mixed))
203+
* )
199204
*
205+
* @since 1.0.0
200206
*/
201207
public function get( $slug = null, $sort = true ) {
202208
$obj = $this;
@@ -249,11 +255,7 @@ public function get( $slug = null, $sort = true ) {
249255
// Prevent weird stuff here.
250256
$slug = sanitize_key( $slug );
251257

252-
if ( ! empty( $this->assets[ $slug ] ) ) {
253-
return $this->assets[ $slug ];
254-
}
255-
256-
return [];
258+
return $this->assets[ $slug ] ?? null;
257259
}
258260

259261
/**
@@ -696,7 +698,7 @@ public function register_in_wp( $assets = null ) {
696698
)
697699
) {
698700
// Registering the asset now would trigger a doing_it_wrong notice: queue the assets to be registered later.
699-
if ( $assets === null || empty( $assets ) ) {
701+
if ( empty( $assets ) ) {
700702
return;
701703
}
702704

@@ -829,9 +831,32 @@ public function remove( $slug ) {
829831
}
830832

831833
/**
832-
* Prints the `script` (JS) and `link` (CSS) HTML tags associated with one or more assets groups.
834+
* Remove all assets.
835+
*
836+
* @since 1.5.0
837+
*
838+
* @return int The number of assets successfully removed.
839+
*/
840+
public function remove_all() {
841+
$removed = 0;
842+
843+
foreach ( $this->assets as $slug => $asset ) {
844+
if ( ! $this->remove( $slug ) ) {
845+
continue;
846+
}
847+
848+
++$removed;
849+
}
850+
851+
return $removed;
852+
}
853+
854+
/**
855+
* Prints the `script` (JS) and `link` (CSS) HTML tags associated with one or more assets
856+
* groups.
833857
*
834-
* The method will force the scripts and styles to print overriding their registration and conditional.
858+
* The method will force the scripts and styles to print overriding their registration and
859+
* conditional.
835860
*
836861
* @since 1.0.0
837862
*

tests/wpunit/AssetsTest.php

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ public function setUp() {
2626
Config::set_relative_asset_path( 'tests/_data/' );
2727
}
2828

29-
public function tearDown() {
30-
parent::tearDown();
29+
protected function tearDown() {
30+
// Remove all assets.
31+
Assets::init()->remove_all();
3132
Config::reset();
33+
parent::tearDown();
3234
}
3335

3436
/**
@@ -47,6 +49,9 @@ public function unset_uopz_redefines() {
4749
self::$uopz_redefines = [];
4850
}
4951

52+
/**
53+
* @test
54+
*/
5055
public function it_should_accept_instance_of_asset_or_array_of_assets_in_register_in_wp() {
5156
$asset_1 = Asset::add( 'fake1-script', 'fake1.js' );
5257
$asset_2 = Asset::add( 'fake1-style', 'fake1.css' );
@@ -56,8 +61,8 @@ public function it_should_accept_instance_of_asset_or_array_of_assets_in_registe
5661

5762
$assets = Assets::init();
5863

59-
$assets->register_in_wp( null ); // No problemo... nothing happens though.
60-
$assets->register_in_wp( [] ); // No problemo... nothing happens though.
64+
// An empty array should not cause any assets to be registered.
65+
$assets->register_in_wp( [] );
6166

6267
$this->assertFalse( $asset_1->is_registered() );
6368
$assets->register_in_wp( $asset_1 );
@@ -74,6 +79,75 @@ public function it_should_accept_instance_of_asset_or_array_of_assets_in_registe
7479
$this->assertTrue( $asset_5->is_registered() );
7580
}
7681

82+
/**
83+
* @test
84+
*/
85+
public function it_should_remove_all_assets() {
86+
Asset::add( 'asset-1', 'asset-1.js' );
87+
Asset::add( 'asset-2', 'asset-2.js' );
88+
Asset::add( 'asset-3', 'asset-3.js' );
89+
90+
$assets = Assets::init();
91+
92+
$this->assertCount( 3, $assets->get() );
93+
$this->assertSame( 3, $assets->remove_all() );
94+
$this->assertCount( 0, $assets->get() );
95+
}
96+
97+
/**
98+
* @test
99+
*/
100+
public function it_should_return_null_when_single_asset_not_found() {
101+
Asset::add( 'asset-1', 'asset-1.js' );
102+
Asset::add( 'asset-2', 'asset-2.js' );
103+
Asset::add( 'asset-3', 'asset-3.js' );
104+
105+
$result = Assets::init()->get( 'unknown-slug' );
106+
$this->assertNull( $result );
107+
}
108+
109+
/**
110+
* @test
111+
*/
112+
public function it_should_return_empty_array_when_no_assets() {
113+
$result = Assets::init()->get();
114+
$this->assertSame( [], $result );
115+
}
116+
117+
/**
118+
* @test
119+
*/
120+
public function it_gets_single_asset() {
121+
Asset::add( 'my-single-asset', 'my-single-asset.js' );
122+
$result = Assets::init()->get( 'my-single-asset' );
123+
$this->assertInstanceOf( Asset::class, $result );
124+
}
125+
126+
/**
127+
* @test
128+
*/
129+
public function it_gets_a_subset_of_assets() {
130+
Asset::add( 'asset-1', 'asset-1.js' );
131+
Asset::add( 'asset-2', 'asset-2.js' );
132+
Asset::add( 'asset-3', 'asset-3.js' );
133+
134+
$assets = Assets::init();
135+
136+
$result = $assets->get( ['asset-2', 'asset-3'] );
137+
$this->assertIsArray( $result );
138+
$this->assertCount( 2, $result );
139+
$this->assertArrayNotHasKey( 'asset-1', $result );
140+
$this->assertArrayHasKey( 'asset-2', $result );
141+
$this->assertArrayHasKey( 'asset-3', $result );
142+
143+
$single = $assets->get( ['asset-1'] );
144+
$this->assertIsArray( $single );
145+
$this->assertCount( 1, $single );
146+
$this->assertArrayHasKey( 'asset-1', $single );
147+
$this->assertArrayNotHasKey( 'asset-2', $single );
148+
$this->assertArrayNotHasKey( 'asset-3', $single );
149+
}
150+
77151
public function invalid_params_for_register_in_wp_provider(): Generator {
78152
yield 'string' => [ fn() => 'string' ];
79153
yield 'int' => [ fn() => 1 ];
@@ -121,7 +195,7 @@ public function it_should_locate_minified_versions_of_external_assets() {
121195
$slugs = [
122196
'fake1' => [ true, false ],
123197
'fake2' => [ false, false ],
124-
'fake3' => [ true, true ]
198+
'fake3' => [ true, true ],
125199
];
126200

127201
foreach ( array_keys( $slugs ) as $slug ) {
@@ -143,7 +217,7 @@ public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content
143217
$slugs = [
144218
'fake1' => [ 'has_min' => true, 'has_only_min' => false ],
145219
'fake2' => [ 'has_min' => false, 'has_only_min' => false ],
146-
'fake3' => [ 'has_min' => true, 'has_only_min' => true ]
220+
'fake3' => [ 'has_min' => true, 'has_only_min' => true ],
147221
];
148222

149223
foreach ( array_keys( $slugs ) as $slug ) {
@@ -183,7 +257,7 @@ public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content
183257
$slugs = [
184258
'fake1' => [ true, false ],
185259
'fake2' => [ false, false ],
186-
'fake3' => [ true, true ]
260+
'fake3' => [ true, true ],
187261
];
188262

189263
foreach ( array_keys( $slugs ) as $slug ) {
@@ -224,7 +298,7 @@ public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content
224298
$slugs = [
225299
'fake1' => [ true, false ],
226300
'fake2' => [ false, false ],
227-
'fake3' => [ true, true ]
301+
'fake3' => [ true, true ],
228302
];
229303

230304
foreach ( array_keys( $slugs ) as $slug ) {
@@ -265,7 +339,7 @@ public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content
265339
$slugs = [
266340
'fake1' => [ 'has_min' => true, 'has_only_min' => false ],
267341
'fake2' => [ 'has_min' => false, 'has_only_min' => false ],
268-
'fake3' => [ 'has_min' => true, 'has_only_min' => true ]
342+
'fake3' => [ 'has_min' => true, 'has_only_min' => true ],
269343
];
270344

271345
foreach ( array_keys( $slugs ) as $slug ) {
@@ -462,7 +536,7 @@ public function should_localize_data_correctly(): void {
462536
->register();
463537
Asset::add( 'my-second-script-mod', 'second-script-mod.js' )
464538
->add_localize_script( 'boomshakalakaProjectSecondScriptModData', [
465-
'animal' => 'horse'
539+
'animal' => 'horse',
466540
] )
467541
->register();
468542

@@ -513,7 +587,7 @@ public function should_localize_dot_notation_data_correctly(): void {
513587
->register();
514588
Asset::add( 'my-second-ns-script-mod', 'second-script-mod.js' )
515589
->add_localize_script( 'boomshakalaka.project.secondScriptData', [
516-
'animal' => 'horse'
590+
'animal' => 'horse',
517591
] )
518592
->register();
519593

0 commit comments

Comments
 (0)