Skip to content

Commit

Permalink
chore: Fix coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
nlemoine committed Nov 14, 2024
1 parent 7a582e9 commit a5870fa
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 43 deletions.
43 changes: 18 additions & 25 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<file>.</file>

<!-- Show progress. -->
<arg value="p"/>
<arg value="ps"/>

<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="./"/>
Expand All @@ -19,29 +19,22 @@
<config name="testVersion" value="7.2-"/>

<!-- Rules: Include the base ruleset for WP-CLI projects. -->
<rule ref="WP_CLI_CS"/>

<!-- Verify that everything in the global namespace is either namespaced or prefixed.
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<property name="prefixes" type="array">
<element value="Hellonico\Fixtures"/><!-- Namespaces. -->
<element value="wp_fixtures"/><!-- Global variables and such. -->
</property>
</properties>
</rule>

<!-- Whitelist property name for a a JSON-decoded object. -->
<rule ref="WordPress.NamingConventions.ValidVariableName">
<properties>
<property name="allowed_custom_properties" type="array">
<element value="sourcesContent"/>
<element value="functionsScannerClass"/>
</property>
</properties>
</rule>

<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses"/>
<rule ref="WP_CLI_CS">
<exclude name="WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase"/>
<exclude name="WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid"/>
</rule>

<!-- Verify that everything in the global namespace is either namespaced or prefixed.
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<property name="prefixes" type="array">
<element value="Hellonico\Fixtures"/><!-- Namespaces. -->
<element value="wp_fixtures"/><!-- Global variables and such. -->
</property>
</properties>
</rule>

<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses"/>

</ruleset>
8 changes: 4 additions & 4 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ public function delete( $args = [], array $assoc_args = [] ) {
/**
* Get content type.
*
* @param object $object
* @param object $obj
*
* @return string
*/
private function getContentType( $object ) {
$reflect = new \ReflectionClass( $object );
private function getContentType( $obj ) {
$reflect = new \ReflectionClass( $obj );
$type = strtolower( $reflect->getShortName() );

if ( 'post' === $type ) {
$type = $object->post_type;
$type = $obj->post_type;
}

return $type;
Expand Down
14 changes: 9 additions & 5 deletions src/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Hellonico\Fixtures\Entity;

use AllowDynamicProperties;

#[AllowDynamicProperties]
abstract class Entity implements EntityInterface {

/**
Expand All @@ -23,7 +26,8 @@ public function __construct( $id = false ) {
$this->{$field} = $value;
}

return $this->exists( $id ) ? $this->setCurrentId( $id ) : $this->setCurrentId( false );
$this->exists( $id ) ? $this->setCurrentId( $id ) : $this->setCurrentId( false );
return;
}
$this->create();
}
Expand All @@ -43,7 +47,7 @@ protected function getData() {
if ( is_array( $value ) ) {
array_walk_recursive(
$value,
function ( &$v, $k ) {
function ( &$v ) {
if ( $v instanceof \DateTime ) {
$v = $v->format( 'Y-m-d H:i:s' );
}
Expand Down Expand Up @@ -100,13 +104,13 @@ protected function filterProperties() {
/**
* Removes null values from array.
*
* @param array $array
* @param array $data
*
* @return array
*/
private function filterData( $array ) {
private function filterData( $data ) {
return array_filter(
$array,
$data,
function ( $v ) {
return ! is_null( $v );
}
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/NavMenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function persist() {
// Handle errors
if ( is_wp_error( $post_id ) ) {
wp_delete_post( $this->ID, true );
WP_CLI::error( html_entity_decode( $post_id->get_error_message() ), false );
WP_CLI::error( $post_id->get_error_message(), false );
WP_CLI::error( sprintf( 'An error occured while updating the post ID %d, it has been deleted.', $this->ID ), false );
$this->setCurrentId( false );

Expand All @@ -70,7 +70,7 @@ private function transformData( array $data ) {
}

// Get properties for objects
if ( $key === 'menu_item_object' && $item instanceof Entity ) {
if ( 'menu_item_object' === $key && $item instanceof Entity ) {
if ( $item instanceof Post ) {
$data['menu-item-type'] = 'post_type';
$data['menu-item-object'] = $item->post_type;
Expand Down
5 changes: 3 additions & 2 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function persist() {
// Handle errors
if ( is_wp_error( $post_id ) ) {
wp_delete_post( $this->ID, true );
WP_CLI::error( html_entity_decode( $post_id->get_error_message() ), false );
WP_CLI::error( $post_id->get_error_message(), false );
WP_CLI::error( sprintf( 'An error occured while updating the post ID %d, it has been deleted.', $this->ID ), false );
$this->setCurrentId( false );

Expand Down Expand Up @@ -99,6 +99,7 @@ public function persist() {
// Add terms
$tt_ids = wp_set_object_terms( $post_id, $terms, $taxonomy );

// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
// Add fake flag to created terms
// foreach ($terms as $term) {
// if (($term_obj = get_term_by('name', $term, $taxonomy)) !== false) {
Expand Down Expand Up @@ -175,7 +176,7 @@ public static function delete() {
$types = array_filter(
$types,
function ( $type ) {
return ! in_array( $type, [ 'attachment', 'nan_menu_item' ] );
return ! in_array( $type, [ 'attachment', 'nan_menu_item' ], true );
}
);

Expand Down
8 changes: 4 additions & 4 deletions src/Entity/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Term extends Entity {
* @param string $taxonomy
*/
public function __construct( $taxonomy = null ) {
if ( $taxonomy === null ) {
if ( null === $taxonomy ) {
$taxonomy = $this->taxonomy;
}
$this->taxonomy = $taxonomy;
Expand All @@ -35,7 +35,7 @@ public function __construct( $taxonomy = null ) {
public function create() {
$term = wp_insert_term( sprintf( 'term-%s', uniqid() ), $this->taxonomy );
if ( is_wp_error( $term ) ) {
WP_CLI::error( html_entity_decode( $term->get_error_message() ), false );
WP_CLI::error( $term->get_error_message(), false );
$this->setCurrentId( false );

return $term;
Expand Down Expand Up @@ -77,7 +77,7 @@ public function persist() {

if ( is_wp_error( $term_id ) ) {
wp_delete_term( $this->term_id, $this->taxonomy );
WP_CLI::error( html_entity_decode( $term_id->get_error_message() ), false );
WP_CLI::error( $term_id->get_error_message(), false );
WP_CLI::error( sprintf( 'An error occured while updating the term ID %d, it has been deleted.', $this->term_id ), false );
$this->setCurrentId( false );

Expand Down Expand Up @@ -156,7 +156,7 @@ public static function delete() {
continue;
}
// Nav Menu's are handled within NavMenu.
if ( $term->taxonomy === 'nav_menu' ) {
if ( 'nav_menu' === $term->taxonomy ) {
continue;
}
wp_delete_term( $id, $term->taxonomy );
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function persist() {
$user_id = wp_update_user( $this->getData() );
if ( is_wp_error( $user_id ) ) {
wp_delete_user( $this->ID );
WP_CLI::error( html_entity_decode( $user_id->get_error_message() ), false );
WP_CLI::error( $user_id->get_error_message(), false );
WP_CLI::error( sprintf( 'An error occured while updating the user ID %d, it has been deleted.', $this->ID ), false );
$this->setCurrentId( false );

Expand Down

0 comments on commit a5870fa

Please sign in to comment.