Skip to content

Commit 66f5b9d

Browse files
authored
Feature/phpstan upgrade (#434)
* Upgrade and first fixes * Issues resolved * Tests added
1 parent 23c82c4 commit 66f5b9d

6 files changed

+28
-25
lines changed

includes/MslsOptionsTax.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,42 @@ class MslsOptionsTax extends MslsOptions implements OptionsTaxInterface {
1919
* @return OptionsTaxInterface
2020
*/
2121
public static function create( $id = 0 ): OptionsTaxInterface {
22-
$id = ! empty( $id ) ? (int) $id : get_queried_object_id();
23-
24-
$req = '';
25-
if ( is_admin() ) {
26-
$req = msls_content_types()->acl_request();
27-
} elseif ( is_category() ) {
28-
$req = 'category';
29-
} elseif ( is_tag( $id ) ) {
30-
$req = 'post_tag';
31-
}
22+
$id = ! empty( $id ) ? (int) $id : get_queried_object_id();
23+
$req = self::get_content_type( $id );
3224

3325
switch ( $req ) {
3426
case 'category':
3527
$options = new MslsOptionsTaxTermCategory( $id );
36-
add_filter( 'msls_get_postlink', array( $options, 'check_base' ), 9, 2 );
3728
break;
3829
case 'post_tag':
3930
$options = new MslsOptionsTaxTerm( $id );
40-
add_filter( 'msls_get_postlink', array( $options, 'check_base' ), 9, 2 );
4131
break;
4232
default:
43-
global $wp_rewrite;
44-
45-
$options = new MslsOptionsTax( $id );
46-
$options->with_front = ! empty( $wp_rewrite->extra_permastructs[ $options->get_tax_query() ]['with_front'] );
33+
$options = new MslsOptionsTax( $id );
4734
}
4835

49-
return $options;
36+
return $options->handle_rewrite();
5037
}
5138

5239
/**
5340
* @param int $id
5441
*
5542
* @return string
5643
*/
57-
public function get_content_type( int $id ): string {
44+
public static function get_content_type( int $id ): string {
5845
if ( is_admin() ) {
5946
return msls_content_types()->acl_request();
6047
}
6148

62-
return ( is_category() ? 'category' : is_tag( $id ) ) ? 'post_tag' : '';
49+
return ( is_category( $id ) ? 'category' : ( is_tag( $id ) ? 'post_tag' : '' ) );
50+
}
51+
52+
public function handle_rewrite(): OptionsTaxInterface {
53+
global $wp_rewrite;
54+
55+
$this->with_front = ! empty( $wp_rewrite->extra_permastructs[ $this->get_tax_query() ]['with_front'] );
56+
57+
return $this;
6358
}
6459

6560
/**

includes/MslsOptionsTaxTerm.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class MslsOptionsTaxTerm extends MslsOptionsTax implements OptionsTaxInterface {
2020
*/
2121
public ?bool $with_front = true;
2222

23+
public function handle_rewrite(): OptionsTaxInterface {
24+
add_filter( 'msls_get_postlink', array( $this, 'check_base' ), 9, 2 );
25+
26+
return $this;
27+
}
28+
2329
/**
2430
* Check and correct URL
2531
*

includes/OptionsTaxInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
interface OptionsTaxInterface extends OptionsInterface {
66

77
public static function get_base_option(): string;
8+
9+
public function handle_rewrite(): OptionsTaxInterface;
810
}

tests/phpunit/TestMslsOptionsTax.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ final class TestMslsOptionsTax extends MslsUnitTestCase {
1212
private function MslsOptionsTaxFactory(): MslsOptionsTax {
1313
Functions\expect( 'get_option' )->atLeast()->once()->andReturn( array( 'de_DE' => 42 ) );
1414

15-
return new MslsOptionsTax( 0 );
15+
return new MslsOptionsTax();
1616
}
1717

1818
public function test_create_category(): void {
1919
Functions\expect( 'get_queried_object_id' )->once()->andReturn( 42 );
2020
Functions\expect( 'is_admin' )->once()->andReturnFalse();
21-
Functions\expect( 'is_category' )->once()->andReturnTrue();
21+
Functions\expect( 'is_category' )->once()->with( 42 )->andReturnTrue();
2222
Functions\expect( 'get_option' )->atLeast()->once()->andReturn( array( 'de_DE' => 42 ) );
2323

2424
$this->assertInstanceOf( MslsOptionsTaxTermCategory::class, MslsOptionsTax::create() );

tests/phpunit/TestMslsPostTag.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function test_edit_input(): void {
8787
$taxonomy = \Mockery::mock( MslsTaxonomy::class );
8888
$taxonomy->shouldReceive( 'is_taxonomy' )->atLeast()->once()->andReturn( true );
8989
$taxonomy->shouldReceive( 'get_request' )->atLeast()->once()->andReturn( 'post' );
90-
$taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( array( 'taxonomy', 'post_tag' ) );
90+
$taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( 'taxonomy' );
9191

9292
$term = \Mockery::mock( \WP_Term::class );
9393
$term->name = 'test-term-name';
@@ -140,7 +140,7 @@ public function test_add_input(): void {
140140
$taxonomy = \Mockery::mock( MslsTaxonomy::class );
141141
$taxonomy->shouldReceive( 'is_taxonomy' )->atLeast()->once()->andReturnTrue();
142142
$taxonomy->shouldReceive( 'get_request' )->atLeast()->once()->andReturn( 'post' );
143-
$taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( array( 'taxonomy', 'post_tag' ) );
143+
$taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( 'taxonomy' );
144144

145145
Functions\expect( 'msls_content_types' )->atLeast()->once()->andReturn( $taxonomy );
146146
Functions\expect( 'get_queried_object_id' )->atLeast()->once()->andReturn( 42 );

tests/phpunit/TestMslsPostTagClassic.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function test_edit_input(): void {
7070
Functions\expect( 'get_edit_term_link' )->atLeast()->once()->andReturn( 'edit_term_link' );
7171

7272
$taxonomy = \Mockery::mock( \WP_Taxonomy::class );
73-
$taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( array( 'taxonomy', 'post_type' ) );
73+
$taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( 'taxonomy' );
7474
$taxonomy->shouldReceive( 'is_taxonomy' )->atLeast()->once()->andReturnTrue();
7575
$taxonomy->shouldReceive( 'get_request' )->atLeast()->once()->andReturn( 'post_type' );
7676

@@ -133,7 +133,7 @@ public function test_add_input(): void {
133133
Functions\expect( 'get_edit_term_link' )->atLeast()->once()->andReturn( 'edit_term_link' );
134134

135135
$taxonomy = \Mockery::mock( \WP_Taxonomy::class );
136-
$taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( array( 'taxonomy', 'post_type' ) );
136+
$taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( 'taxonomy' );
137137
$taxonomy->shouldReceive( 'is_taxonomy' )->atLeast()->once()->andReturnTrue();
138138
$taxonomy->shouldReceive( 'get_request' )->atLeast()->once()->andReturn( 'post_type' );
139139

0 commit comments

Comments
 (0)