Skip to content

Commit

Permalink
Upgrading to Alley Coding Standards 2.0 (#585)
Browse files Browse the repository at this point in the history
* Starting migration to cs 2.0

* Contd work

* PHPCS fixes for database

* Contd migration

* Fixing testing

* Migrating helper

* Finishing upgrade of coding standards

* Removing some global rule exceptions

* Removing some global rule exceptions

* Finishing upgrade

* Fixing tests
  • Loading branch information
srtfisher authored Sep 17, 2024
1 parent ff6d71c commit 994a328
Show file tree
Hide file tree
Showing 117 changed files with 452 additions and 447 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"voku/portable-ascii": "^2.0.1"
},
"require-dev": {
"alleyinteractive/alley-coding-standards": "^1.0.1",
"alleyinteractive/alley-coding-standards": "^2.0",
"alleyinteractive/wp-match-blocks": "^1.0 || ^2.0 || ^3.0",
"guzzlehttp/guzzle": "^7.7",
"league/flysystem-aws-s3-v3": "^3.15",
Expand Down
2 changes: 1 addition & 1 deletion monorepo-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
'php' => '^8.1',
],
ComposerJsonSection::REQUIRE_DEV => [
'alleyinteractive/alley-coding-standards' => '^1.0',
'alleyinteractive/alley-coding-standards' => '^2.0',
'phpunit/phpunit' => '^9.3.3 || ^10.0.7 || ^11.0',
],
],
Expand Down
12 changes: 5 additions & 7 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,21 @@
<arg name="parallel" value="20" />

<!-- Set severity to 1 to see everything that isn't effectively turned off. -->
<!-- todo: enable in a follow up pull request -->
<!-- <arg name="severity" value="1" /> -->
<arg name="severity" value="1" />

<exclude-pattern>bin/</exclude-pattern>
<exclude-pattern>tests/</exclude-pattern>

<rule ref="Alley-Interactive">
<exclude name="WordPress.NamingConventions.ValidPostTypeSlug.NotStringLiteral" />
<exclude name="WordPress.PHP.DisallowShortTernary.Found" />
<exclude name="WordPress.WP.CapitalPDangit.Misspelled" />

<exclude name="WordPress.Security.EscapeOutput.ExceptionNotEscaped" />
<exclude name="WordPress.WP.CapitalPDangit.MisspelledInText" />
<exclude name="PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection" />
<exclude name="PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed" />
<exclude name="Squiz.Commenting.FunctionComment.IncorrectTypeHint" />
<exclude name="Generic.Commenting.DocComment.SpacingAfterTagGroup" />
<exclude name="Generic.Commenting.DocComment.MissingShort" />
<exclude name="Squiz.Commenting.VariableComment.MissingVar" />
<exclude name="Generic.CodeAnalysis.UnusedFunctionParameter.Found" />
<exclude name="Generic.CodeAnalysis.AssignmentInCondition.Found" />
</rule>

<rule ref="Generic.Arrays.DisallowLongArraySyntax" />
Expand Down
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
/**
* Rector Configuration
*
* phpcs:disable
*/

Expand Down
12 changes: 6 additions & 6 deletions src/mantle/application/class-application.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
* Mantle Application
*/
class Application extends Container implements \Mantle\Contracts\Application {
use Concerns\Application_Callbacks,
Concerns\Loads_Base_Configuration,
Concerns\Loads_Environment_Variables,
Concerns\Loads_Facades,
Concerns\Manages_Service_Providers;
use Concerns\Application_Callbacks;
use Concerns\Loads_Base_Configuration;
use Concerns\Loads_Environment_Variables;
use Concerns\Loads_Facades;
use Concerns\Manages_Service_Providers;

/**
* Base path of the application.
Expand Down Expand Up @@ -99,7 +99,7 @@ class Application extends Container implements \Mantle\Contracts\Application {
public function __construct( ?string $base_path = null, string $root_url = null ) {
if ( empty( $base_path ) ) {
$base_path = match ( true ) {
isset( $_ENV['MANTLE_BASE_PATH'] ) => $_ENV['MANTLE_BASE_PATH'],
isset( $_ENV['MANTLE_BASE_PATH'] ) => $_ENV['MANTLE_BASE_PATH'], // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
defined( 'MANTLE_BASE_DIR' ) => MANTLE_BASE_DIR,
default => '',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function load_base_configuration(): void {

// Check if a cached configuration file exists. If found, load it.
if ( is_file( $cached ) ) {
$items = require $cached;
$items = require $cached; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable

$loaded_from_cache = true;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/assets/class-asset-service-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function register(): void {
protected function load_blocks(): void {
foreach ( $this->app['asset.loader']->blocks() as $file ) {
if ( file_exists( $file ) && 0 === validate_file( $file ) ) {
require_once $file;
require_once $file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/mantle/blocks/class-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ public function register(): void {

add_action(
'enqueue_block_editor_assets',
function(): void {
function (): void {
$this->register_editor_assets();
$this->register_frontend_assets();
}
);

add_action(
'init',
function(): void {
function (): void {
$args = wp_parse_args(
[
'attributes' => $this->get_attributes(),
Expand Down
4 changes: 2 additions & 2 deletions src/mantle/console/class-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
* CLI Command for Service Providers
*/
abstract class Command extends Symfony_Command {
use Concerns\Interacts_With_IO,
Macroable;
use Concerns\Interacts_With_IO;
use Macroable;

/**
* Prefix for the command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function forget( $event, $listener = null, int $priority = 10 ): void {
} else {
$this->listeners[ $event ][ $priority ] = array_filter(
$this->listeners[ $event ][ $priority ],
fn ( $value) => $value !== $listener
fn ( $value ) => $value !== $listener
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/mantle/container/class-container.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public function bind( $abstract, $concrete = null, $shared = false ): void {
*/
protected function get_closure( $abstract, $concrete ) {
return function ( $container, $parameters = [] ) use ( $abstract, $concrete ) {
if ( $abstract == $concrete ) {
if ( $abstract === $concrete ) {
return $container->build( $concrete );
}

Expand Down Expand Up @@ -390,7 +390,7 @@ protected function remove_abstract_alias( $searched ) {

foreach ( $this->abstract_aliases as $abstract => $aliases ) {
foreach ( $aliases as $index => $alias ) {
if ( $alias == $searched ) {
if ( $alias === $searched ) {
unset( $this->abstract_aliases[ $abstract ][ $index ] );
}
}
Expand Down Expand Up @@ -870,7 +870,7 @@ protected function resolve_variadic_class( ReflectionParameter $parameter ) {
}

return array_map(
fn ( $abstract) => $this->resolve( $abstract ),
fn ( $abstract ) => $this->resolve( $abstract ),
$concrete
);
}
Expand Down
3 changes: 1 addition & 2 deletions src/mantle/contracts/database/interface-model-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ public function get_meta( string $meta_key, bool $single = true ): mixed;
*
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value to store.
* @param string $prev_value Optional, previous meta value.
*/
public function add_meta( string $meta_key, mixed $meta_value, mixed $prev_value = '' ): void;
public function add_meta( string $meta_key, mixed $meta_value ): void;

/**
* Update meta value for the object.
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/contracts/filesystem/interface-filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function prepend( string $path, string $data, string $separator = PHP_EOL
* @param string $separator Separator from existing data.
* @return bool
*/
public function append( $path, $data, $separator = PHP_EOL);
public function append( $path, $data, $separator = PHP_EOL );

/**
* Retrieve a file's visibility.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function jsonp( $callback, $data = [], $status = 200, array $headers = []
* @param array $headers
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function stream( $callback, $status = 200, array $headers = []);
public function stream( $callback, $status = 200, array $headers = [] );

/**
* Create a new streamed response instance as a file download.
Expand Down Expand Up @@ -107,7 +107,7 @@ public function download( $file, $name = null, array $headers = [], $disposition
* @param array $headers
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function file( $file, array $headers = []);
public function file( $file, array $headers = [] );

/**
* Create a new redirect response to the given path.
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/contracts/interface-container.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function singleton_if( $abstract, $concrete = null );
* @param string $abstract Abstract name.
* @param \Closure $closure Closure callback.
*/
public function extend( $abstract, Closure $closure);
public function extend( $abstract, Closure $closure );

/**
* Register an existing instance as shared in the container.
Expand Down
19 changes: 12 additions & 7 deletions src/mantle/contracts/support/interface-jsonable.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<?php
/**
* Jsonable interface file.
*
* @package Mantle
*/

namespace Mantle\Contracts\Support;

interface Jsonable {
/**
* Convert the object to its JSON representation.
*
* @param int $options json_encode() options.
* @return string
*/
public function to_json( $options = 0 );
/**
* Convert the object to its JSON representation.
*
* @param int $options json_encode() options.
* @return string
*/
public function to_json( $options = 0 );
}
2 changes: 1 addition & 1 deletion src/mantle/database/model/class-attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static function create_from_url( string $url, array $args = [] ): Model {

// If error storing permanently, unlink.
if ( \is_wp_error( $attachment_id ) ) {
@unlink( $file_array['tmp_name'] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_unlink
@unlink( $file_array['tmp_name'] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_unlink, Generic.PHP.NoSilencedErrors.Forbidden
throw new Model_Exception( $attachment_id->get_error_message() );
}

Expand Down
4 changes: 2 additions & 2 deletions src/mantle/database/model/class-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* @method static \Mantle\Database\Factory\Post_Factory<static, \WP_Comment, static> factory( array|callable|null $state = null )
*/
class Comment extends Model implements Contracts\Database\Core_Object, Contracts\Database\Model_Meta, Contracts\Database\Updatable {
use Meta\Model_Meta,
Meta\Comment_Meta;
use Meta\Model_Meta;
use Meta\Comment_Meta;

/**
* Attributes for the model from the object
Expand Down
42 changes: 23 additions & 19 deletions src/mantle/database/model/class-model-not-found-exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,26 @@
*/
class Model_Not_Found_Exception extends Model_Exception {
/**
* Name of the affected Eloquent model.
* Constructor.
*
* @var string
* @param array|string $model Name of the affected Eloquent model(s).
* @param array $ids Model ID(s).
*/
protected $model;

/**
* The affected model IDs.
*
* @var int|array
*/
protected $ids;
public function __construct( public array|string $model, public array $ids = [] ) {
$this->set_message();
}

/**
* Set the affected Eloquent model and instance ids.
*
* @param string $model Model name.
* @param int|array $ids Model ID(s).
* @return static
*/
public function set_model( string $model, $ids = [] ) {
public function set_model( string $model, $ids = [] ): static {
$this->model = $model;
$this->ids = Arr::wrap( $ids );

$this->message = "No query results for model [{$model}]";

if ( count( $this->ids ) > 0 ) {
$this->message .= ' ' . implode( ', ', $this->ids );
} else {
$this->message .= '.';
}
$this->set_message();

return $this;
}
Expand All @@ -64,4 +53,19 @@ public function get_model(): string {
public function get_ids() {
return $this->ids;
}

/**
* Set the exception message.
*/
protected function set_message(): void {
$model = is_array( $this->model ) ? implode( ', ', $this->model ) : $this->model;

$this->message = "No query results for model [{$model}]";

if ( count( $this->ids ) > 0 ) {
$this->message .= ' ' . implode( ', ', $this->ids );
} else {
$this->message .= '.';
}
}
}
20 changes: 10 additions & 10 deletions src/mantle/database/model/class-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
* @method static \Mantle\Contracts\Paginator\Paginator paginate(int $per_page = 20, int $current_page = null)
*/
abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable, Url_Routable {
use Forward_Calls,
Concerns\Has_Aliases,
Concerns\Has_Attributes,
Concerns\Has_Events,
/** @use Concerns\Has_Factory<TModelObject> */
Concerns\Has_Factory,
Concerns\Has_Global_Scopes,
Concerns\Has_Relationships;
use Forward_Calls;
use Concerns\Has_Aliases;
use Concerns\Has_Attributes;
use Concerns\Has_Events;
/** @use Concerns\Has_Factory<TModelObject> */
use Concerns\Has_Factory;
use Concerns\Has_Global_Scopes;
use Concerns\Has_Relationships;

/**
* The array of booted models.
Expand Down Expand Up @@ -131,7 +131,7 @@ public static function find_or_fail( $object ) {
return $find;
}

throw ( new Model_Not_Found_Exception() )->set_model( self::class, $object );
throw new Model_Not_Found_Exception( static::class, [ $object ] );
}

/**
Expand Down Expand Up @@ -294,7 +294,7 @@ protected static function boot_traits() {
$trait_method = strtolower( class_basename( $trait ) );
$method = 'boot_' . $trait_method;

if ( method_exists( $class, $method ) && ! in_array( $method, $booted ) ) {
if ( method_exists( $class, $method ) && ! in_array( $method, $booted, true ) ) {
forward_static_call( [ $class, $method ] );

$booted[] = $method;
Expand Down
Loading

0 comments on commit 994a328

Please sign in to comment.