Skip to content

Commit

Permalink
CS: enforce consistent array format
Browse files Browse the repository at this point in the history
WordPressCS will demand long arrays and forbid the use of short arrays as of WPCS 2.2.0.
See: https://make.wordpress.org/core/2019/07/12/php-coding-standards-changes/

For YoastCS, however, a choice has been made to demand short arrays.

For now, while WP 4.9, and therefore PHP 5.2 still needs to be supported, this is only possible in the directories containing files with PHP 5.6+ code.
So in this interim period, we will enforce short arrays in the PHP 5.6+ code and long arrays everywhere else.

Once the minimum supported WP version has moved up to WP 5.2 / PHP 5.6, short arrays will be enforced for all code and the change-over can be done automatically using the auto-fixer included in the relevant sniff.
  • Loading branch information
jrfnl committed Aug 7, 2019
1 parent a7ac20a commit bb396f9
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 63 deletions.
26 changes: 26 additions & 0 deletions phpcs.xml.dist
Expand Up @@ -136,4 +136,30 @@
<rule ref="PHPCompatibility.FunctionUse.NewFunctionParameters.array_unique_sort_flagsFound">
<exclude-pattern>*/inc/sitemaps/class-sitemap-image-parser\.php$</exclude-pattern>
</rule>

<!--
#############################################################################
TEMPORARY TWEAK
YoastCS will demand short arrays, but until support for WP < 5.2 has been
dropped, this can - for now - only be allowed (and enforced) for the folders
containing PHP 5.6+ code.
#############################################################################
-->

<rule ref="Generic.Arrays.DisallowShortArraySyntax">
<exclude-pattern>*/config/*.php$</exclude-pattern>
<exclude-pattern>*/src/*.php$</exclude-pattern>
<exclude-pattern>*/tests/*.php$</exclude-pattern>
</rule>
<rule ref="Generic.Arrays.DisallowLongArraySyntax">
<exclude-pattern>*/wp-seo-main\.php$</exclude-pattern>
<exclude-pattern>*/admin/*.php$</exclude-pattern>
<exclude-pattern>*/cli/*.php$</exclude-pattern>
<exclude-pattern>*/frontend/*.php$</exclude-pattern>
<exclude-pattern>*/inc/*.php$</exclude-pattern>
<exclude-pattern>*/integration-tests/*.php$</exclude-pattern>
<exclude-pattern>*/migrations/*.php$</exclude-pattern>
<exclude-pattern>*/scripts/*.php$</exclude-pattern>
</rule>

</ruleset>
2 changes: 1 addition & 1 deletion src/loggers/logger.php
Expand Up @@ -47,7 +47,7 @@ public function __construct() {
*
* @return void
*/
public function log( $level, $message, array $context = array() ) {
public function log( $level, $message, array $context = [] ) {
$this->wrapped_logger->log( $level, $message, $context );
}
}
4 changes: 2 additions & 2 deletions src/orm/yoast-model.php
Expand Up @@ -482,11 +482,11 @@ protected function has_many_through( $associated_class_name, $join_class_name =
->select( "{$associated_table_name}.*" )
->join(
$join_table_name,
array(
[
"{$associated_table_name}.{$associated_table_id_column}",
'=',
"{$join_table_name}.{$key_to_associated_table}",
)
]
)
->where( "{$join_table_name}.{$key_to_base_table}", $this->{$base_table_id_column} );
}
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Expand Up @@ -92,7 +92,7 @@ protected function expectOutputContains( $expected ) {
\ob_clean();

if ( ! \is_array( $expected ) ) {
$expected = array( $expected );
$expected = [ $expected ];
}

foreach ( $expected as $needle ) {
Expand All @@ -111,7 +111,7 @@ protected function expectOutputNotContains( $needles ) {
\ob_clean();

if ( ! \is_array( $needles ) ) {
$needles = array( $needles );
$needles = [ $needles ];
}

foreach ( $needles as $needle ) {
Expand Down
14 changes: 7 additions & 7 deletions tests/admin/admin-features-test.php
Expand Up @@ -49,11 +49,11 @@ public function test_get_admin_features_ON_post_edit() {

$class_instance = $this->get_admin_with_expectations();

$admin_features = array(
$admin_features = [
'google_search_console' => new WPSEO_GSC(),
'primary_category' => new WPSEO_Primary_Term_Admin(),
'dashboard_widget' => new Yoast_Dashboard_Widget(),
);
];

$this->assertEquals( $admin_features, $class_instance->get_admin_features() );
}
Expand All @@ -69,10 +69,10 @@ public function test_get_admin_features_NOT_ON_post_edit() {

$class_instance = $this->get_admin_with_expectations();

$admin_features = array(
$admin_features = [
'google_search_console' => new WPSEO_GSC(),
'dashboard_widget' => new Yoast_Dashboard_Widget(),
);
];

$this->assertEquals( $admin_features, $class_instance->get_admin_features() );
}
Expand All @@ -82,11 +82,11 @@ public function test_get_admin_features_NOT_ON_post_edit() {
*/
public function test_update_contactmethods() {
$class_instance = $this->get_admin_with_expectations();
$result = $class_instance->update_contactmethods( array() );
$result = $class_instance->update_contactmethods( [] );
\ksort( $result );

$this->assertSame(
array(
[
'facebook' => 'Facebook profile URL',
'instagram' => 'Instagram profile URL',
'linkedin' => 'LinkedIn profile URL',
Expand All @@ -97,7 +97,7 @@ public function test_update_contactmethods() {
'twitter' => 'Twitter username (without @)',
'wikipedia' => 'Wikipedia page about you<br/><small>(if one exists)</small>',
'youtube' => 'YouTube profile URL',
),
],
$result
);
}
Expand Down
20 changes: 10 additions & 10 deletions tests/admin/metabox/metabox-editor-test.php
Expand Up @@ -62,36 +62,36 @@ public function test_add_css_inside_editor_preexisting() {
}

public function test_add_custom_element() {
$expected = array(
$expected = [
'custom_elements' => '~yoastmark',
);
];

$actual = $this->subject->add_custom_element( array() );
$actual = $this->subject->add_custom_element( [] );

$this->assertSame( $expected, $actual );
}

public function test_add_custom_element_preexisting() {
$expected = array(
$expected = [
'custom_elements' => 'div,~yoastmark',
);
];

$actual = $this->subject->add_custom_element( array( 'custom_elements' => 'div' ) );
$actual = $this->subject->add_custom_element( [ 'custom_elements' => 'div' ] );

$this->assertSame( $expected, $actual );
}

public function test_add_custom_element_other_properties() {
$expected = array(
$expected = [
'custom_elements' => '~yoastmark',
'other_property' => 'hello world',
);
];

$actual = $this->subject->add_custom_element(
array(
[
'custom_elements' => '',
'other_property' => 'hello world',
)
]
);
ksort( $actual );

Expand Down
16 changes: 8 additions & 8 deletions tests/admin/myyoast-proxy-test.php
Expand Up @@ -25,7 +25,7 @@ public function test_determine_proxy_options_for_the_research_webworker_file() {
/** @var \Yoast\WP\Free\Tests\Doubles\Admin\MyYoast_Proxy_Double $instance */
$instance = $this
->getMockBuilder( MyYoast_Proxy_Double::class )
->setMethods( array( 'get_proxy_file', 'get_plugin_version' ) )
->setMethods( [ 'get_proxy_file', 'get_plugin_version' ] )
->getMock();

$instance->expects( $this->once() )
Expand All @@ -36,10 +36,10 @@ public function test_determine_proxy_options_for_the_research_webworker_file() {
->method( 'get_plugin_version' )
->will( $this->returnValue( '1.0' ) );

$expected = array(
$expected = [
'content_type' => 'text/javascript; charset=UTF-8',
'url' => 'https://my.yoast.com/api/downloads/file/analysis-worker?plugin_version=1.0',
);
];

$this->assertEquals( $expected, $instance->determine_proxy_options() );
}
Expand All @@ -52,7 +52,7 @@ public function test_render_proxy_page_for_an_unknown_file() {
/** @var \WPSEO_MyYoast_Proxy $instance */
$instance = $this
->getMockBuilder( WPSEO_MyYoast_Proxy::class )
->setMethods( array( 'get_proxy_file', 'get_plugin_version', 'set_header' ) )
->setMethods( [ 'get_proxy_file', 'get_plugin_version', 'set_header' ] )
->getMock();

$instance->expects( $this->once() )
Expand All @@ -77,7 +77,7 @@ public function test_render_proxy_page_for_the_research_webworker_file() {
/** @var \WPSEO_MyYoast_Proxy $instance */
$instance = $this
->getMockBuilder( WPSEO_MyYoast_Proxy::class )
->setMethods( array( 'get_proxy_file', 'get_plugin_version', 'should_load_url_directly', 'set_header', 'load_url' ) )
->setMethods( [ 'get_proxy_file', 'get_plugin_version', 'should_load_url_directly', 'set_header', 'load_url' ] )
->getMock();

$instance
Expand Down Expand Up @@ -132,7 +132,7 @@ public function test_render_proxy_page_for_the_research_webworker_file_errored_a
/** @var \WPSEO_MyYoast_Proxy $instance */
$instance = $this
->getMockBuilder( WPSEO_MyYoast_Proxy::class )
->setMethods( array( 'get_proxy_file', 'get_plugin_version', 'should_load_url_directly', 'set_header', 'load_url' ) )
->setMethods( [ 'get_proxy_file', 'get_plugin_version', 'should_load_url_directly', 'set_header', 'load_url' ] )
->getMock();

$instance
Expand Down Expand Up @@ -207,7 +207,7 @@ public function test_render_proxy_page_via_wordpress() {
/** @var \WPSEO_MyYoast_Proxy $instance */
$instance = $this
->getMockBuilder( WPSEO_MyYoast_Proxy::class )
->setMethods( array( 'get_proxy_file', 'get_plugin_version', 'should_load_url_directly', 'set_header', 'load_url' ) )
->setMethods( [ 'get_proxy_file', 'get_plugin_version', 'should_load_url_directly', 'set_header', 'load_url' ] )
->getMock();

$instance
Expand Down Expand Up @@ -244,7 +244,7 @@ public function test_render_proxy_page_via_wordpress_errored() {
/** @var \WPSEO_MyYoast_Proxy $instance */
$instance = $this
->getMockBuilder( WPSEO_MyYoast_Proxy::class )
->setMethods( array( 'get_proxy_file', 'get_plugin_version', 'should_load_url_directly', 'set_header', 'load_url' ) )
->setMethods( [ 'get_proxy_file', 'get_plugin_version', 'should_load_url_directly', 'set_header', 'load_url' ] )
->getMock();

$instance
Expand Down
10 changes: 5 additions & 5 deletions tests/admin/roles/role-manager-test.php
Expand Up @@ -17,7 +17,7 @@ public function test_register() {

$this->assertNotContains( 'role', $instance->get_roles() );

$instance->register( 'role', 'My Role', array() );
$instance->register( 'role', 'My Role', [] );

$this->assertContains( 'role', $instance->get_roles() );
}
Expand All @@ -28,7 +28,7 @@ public function test_get_capabilities() {
Monkey\Functions\expect( 'get_role' )
->once()
->with( 'administrator' )
->andReturn( (object) array( 'capabilities' => array( 'manage_options' => true ) ) );
->andReturn( (object) [ 'capabilities' => [ 'manage_options' => true ] ] );

$capabilities = $instance->get_capabilities( 'administrator' );

Expand All @@ -46,12 +46,12 @@ public function test_get_capabilities_bad_input() {
->andReturn( false );

$result = $instance->get_capabilities( false );
$this->assertSame( array(), $result );
$this->assertSame( [], $result );

$result = $instance->get_capabilities( new stdClass() );
$this->assertSame( array(), $result );
$this->assertSame( [], $result );

$result = $instance->get_capabilities( 'fake_role' );
$this->assertSame( array(), $result );
$this->assertSame( [], $result );
}
}
4 changes: 2 additions & 2 deletions tests/admin/watchers/indexable-author-watcher-test.php
Expand Up @@ -47,8 +47,8 @@ public function test_register_hooks() {
$instance = new Indexable_Author_Watcher( $repository_mock, new Indexable_Author_Builder() );
$instance->register_hooks();

$this->assertNotFalse( \has_action( 'profile_update', array( $instance, 'build_indexable' ) ) );
$this->assertNotFalse( \has_action( 'deleted_user', array( $instance, 'delete_indexable' ) ) );
$this->assertNotFalse( \has_action( 'profile_update', [ $instance, 'build_indexable' ] ) );
$this->assertNotFalse( \has_action( 'deleted_user', [ $instance, 'delete_indexable' ] ) );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/admin/watchers/indexable-post-watcher-test.php
Expand Up @@ -49,8 +49,8 @@ public function test_register_hooks() {
$instance = new Indexable_Post_Watcher( $repository_mock, $builder_mock );
$instance->register_hooks();

$this->assertNotFalse( \has_action( 'wp_insert_post', array( $instance, 'build_indexable' ) ) );
$this->assertNotFalse( \has_action( 'delete_post', array( $instance, 'delete_indexable' ) ) );
$this->assertNotFalse( \has_action( 'wp_insert_post', [ $instance, 'build_indexable' ] ) );
$this->assertNotFalse( \has_action( 'delete_post', [ $instance, 'delete_indexable' ] ) );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/admin/watchers/indexable-term-watcher-test.php
Expand Up @@ -48,8 +48,8 @@ public function test_register_hooks() {
$instance = new Indexable_Term_Watcher( $repository_mock, $builder_mock );
$instance->register_hooks();

$this->assertNotFalse( \has_action( 'edited_term', array( $instance, 'build_indexable' ) ) );
$this->assertNotFalse( \has_action( 'delete_term', array( $instance, 'delete_indexable' ) ) );
$this->assertNotFalse( \has_action( 'edited_term', [ $instance, 'build_indexable' ] ) );
$this->assertNotFalse( \has_action( 'delete_term', [ $instance, 'delete_indexable' ] ) );
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/config/dependency-management-test.php
Expand Up @@ -22,7 +22,7 @@ class Dependency_Management_Test extends TestCase {
public function test_ensure_class_alias() {
$instance = $this
->getMockBuilder( '\Yoast\WP\Free\Config\Dependency_Management' )
->setMethods( array( 'prefixed_available', 'class_exists', 'class_alias' ) )
->setMethods( [ 'prefixed_available', 'class_exists', 'class_alias' ] )
->getMock();

$instance
Expand Down Expand Up @@ -53,7 +53,7 @@ public function test_ensure_class_alias() {
public function test_ensure_class_alias_unrelated_class() {
$instance = $this
->getMockBuilder( '\Yoast\WP\Free\Config\Dependency_Management' )
->setMethods( array( 'prefixed_available', 'class_exists', 'class_alias' ) )
->setMethods( [ 'prefixed_available', 'class_exists', 'class_alias' ] )
->getMock();

$instance
Expand All @@ -80,7 +80,7 @@ public function test_ensure_class_alias_unrelated_class() {
public function test_ensure_class_alias_prefix_available() {
$instance = $this
->getMockBuilder( '\Yoast\WP\Free\Config\Dependency_Management' )
->setMethods( array( 'prefixed_available', 'class_exists', 'class_alias' ) )
->setMethods( [ 'prefixed_available', 'class_exists', 'class_alias' ] )
->getMock();

$instance
Expand Down Expand Up @@ -108,7 +108,7 @@ public function test_ensure_class_alias_prefix_available() {
public function test_ensure_class_alias_base_class_does_not_exist() {
$instance = $this
->getMockBuilder( '\Yoast\WP\Free\Config\Dependency_Management' )
->setMethods( array( 'prefixed_available', 'class_exists', 'class_alias' ) )
->setMethods( [ 'prefixed_available', 'class_exists', 'class_alias' ] )
->getMock();

$instance
Expand Down Expand Up @@ -141,6 +141,6 @@ public function test_registration_of_autoloader() {

$registered_autoloaders = \spl_autoload_functions();

$this->assertContains( array( $instance, 'ensure_class_alias' ), $registered_autoloaders );
$this->assertContains( [ $instance, 'ensure_class_alias' ], $registered_autoloaders );
}
}

0 comments on commit bb396f9

Please sign in to comment.