From c5892c9d3c73f980c7fe84aa3ab634242d1b585e Mon Sep 17 00:00:00 2001 From: Bart Jaskulski <bjaskulski@protonmail.com> Date: Mon, 30 Sep 2024 21:35:35 +0200 Subject: [PATCH] refactor: upgrade to php 7.4, property types, etc Signed-off-by: Bart Jaskulski <bjaskulski@protonmail.com> --- phpcs.xml.dist | 14 +++++++- src/Binding/Binder/CallableBinder.php | 5 ++- src/Binding/Binder/CompositeBinder.php | 2 +- src/Binding/Binder/HookableBinder.php | 3 +- src/Binding/Binder/ObservableBinder.php | 5 ++- src/Binding/Binder/StoppableBinder.php | 5 ++- src/Binding/Definition/CallableDefinition.php | 3 +- .../Definition/DefinitionCollection.php | 2 +- src/Binding/Definition/UnknownDefinition.php | 3 +- src/Binding/Hookable.php | 1 + src/Binding/Loader/ArrayDefinitions.php | 10 +++--- src/Binding/Loader/CompositeBindingLoader.php | 2 +- src/Binding/Loader/DebugBindingLoader.php | 2 +- src/Binding/Loader/FilesystemDefinitions.php | 9 ++--- src/Binding/Loader/OrderedBindingLoader.php | 2 +- src/Configuration/Configuration.php | 2 +- src/DependencyInjection/ContainerBuilder.php | 3 +- .../CustomOrdersTableCompatibility.php | 6 ++-- src/Extension/CommonBinding/I18n.php | 3 +- .../CommonBinding/WPDeskLicenseBridge.php | 2 +- .../CommonBinding/WPDeskTrackerBridge.php | 3 +- src/Extension/ConfigExtension.php | 4 +-- src/Extension/ExtensionsSet.php | 4 +-- src/HookDriver/CompositeDriver.php | 2 +- src/HookDriver/GenericDriver.php | 8 ++--- src/HookDriver/Legacy/HooksRegistry.php | 18 ++++------ src/HookDriver/LegacyDriver.php | 3 +- src/Init.php | 3 +- src/Kernel.php | 14 +++----- src/Plugin/Header.php | 3 +- src/Plugin/Plugin.php | 33 +++++-------------- src/Util/Path.php | 15 ++++----- src/Util/PhpFileDumper.php | 3 +- src/di-functions.php | 1 + src/platform_check.php | 2 +- tests/DefaultHeaderParserTest.php | 11 ++++--- tests/Dumper/PhpFileDumperTest.php | 2 +- tests/HookDriver/GenericDriverTest.php | 2 +- tests/Loader/PhpFileLoaderTest.php | 2 +- tests/generated/plugin.php | 3 +- 40 files changed, 93 insertions(+), 127 deletions(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 05dfc78..0f86d58 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -36,7 +36,7 @@ <config name="minimum_supported_wp_version" value="6.2"/> <!-- Set value aligned with supported PHP Version for PHPCompatibilityWP check. --> - <config name="testVersion" value="7.2-"/> + <config name="testVersion" value="7.4-"/> <rule ref="WPDeskPlugin"/> @@ -48,4 +48,16 @@ <rule ref="Squiz.ControlStructures.InlineIfDeclaration"> <exclude name="Squiz.ControlStructures.InlineIfDeclaration.NoBrackets"/> </rule> + + <rule ref="Squiz.Commenting.ClassComment.Missing"> + <exclude-pattern>*src/**/*.php$</exclude-pattern> + <exclude-pattern>*src/*.php$</exclude-pattern> + </rule> + <rule ref="Squiz.Commenting.VariableComment.MissingVar"> + <exclude-pattern>*src/**/*.php$</exclude-pattern> + </rule> + <rule ref="Squiz.Commenting.VariableComment.Missing"> + <exclude-pattern>*src/**/*.php$</exclude-pattern> + <exclude-pattern>*src/*.php$</exclude-pattern> + </rule> </ruleset> diff --git a/src/Binding/Binder/CallableBinder.php b/src/Binding/Binder/CallableBinder.php index f4f2ade..291b9e5 100644 --- a/src/Binding/Binder/CallableBinder.php +++ b/src/Binding/Binder/CallableBinder.php @@ -9,10 +9,9 @@ use WPDesk\Init\Binding\ComposableBinder; use WPDesk\Init\Binding\Definition; use WPDesk\Init\Binding\Definition\CallableDefinition; -class CallableBinder implements ComposableBinder { +final class CallableBinder implements ComposableBinder { - /** @var ContainerInterface */ - private $container; + private ContainerInterface $container; public function __construct( ContainerInterface $c ) { $this->container = $c; diff --git a/src/Binding/Binder/CompositeBinder.php b/src/Binding/Binder/CompositeBinder.php index 9ee8eec..febe915 100644 --- a/src/Binding/Binder/CompositeBinder.php +++ b/src/Binding/Binder/CompositeBinder.php @@ -12,7 +12,7 @@ use WPDesk\Init\Binding\Definition\HookableDefinition; final class CompositeBinder implements Binder { /** @var ComposableBinder[] */ - private $binders; + private array $binders; public function __construct( ComposableBinder ...$binders ) { $this->binders = $binders; diff --git a/src/Binding/Binder/HookableBinder.php b/src/Binding/Binder/HookableBinder.php index f8426da..88b9da4 100644 --- a/src/Binding/Binder/HookableBinder.php +++ b/src/Binding/Binder/HookableBinder.php @@ -11,8 +11,7 @@ use WPDesk\Init\Binding\Definition\HookableDefinition; class HookableBinder implements ComposableBinder { - /** @var ContainerInterface */ - private $container; + private ContainerInterface $container; public function __construct( ContainerInterface $c ) { $this->container = $c; diff --git a/src/Binding/Binder/ObservableBinder.php b/src/Binding/Binder/ObservableBinder.php index 0f05c03..9aebce1 100644 --- a/src/Binding/Binder/ObservableBinder.php +++ b/src/Binding/Binder/ObservableBinder.php @@ -13,10 +13,9 @@ use WPDesk\Init\Binding\Definition; */ final class ObservableBinder implements ComposableBinder { - /** @var Binder */ - private $binder; + private Binder $binder; - private $binds_count = 0; + private int $binds_count = 0; public function __construct( Binder $b ) { $this->binder = $b; diff --git a/src/Binding/Binder/StoppableBinder.php b/src/Binding/Binder/StoppableBinder.php index 8917f78..d1b1500 100644 --- a/src/Binding/Binder/StoppableBinder.php +++ b/src/Binding/Binder/StoppableBinder.php @@ -13,13 +13,12 @@ use WPDesk\Init\Binding\Definition\HookableDefinition; class StoppableBinder implements ComposableBinder { - /** @var ContainerInterface */ - private $container; + private ContainerInterface $container; /** @var Binder */ private $binder; - private $should_stop = false; + private bool $should_stop = false; public function __construct( BinderInstance $b, ContainerInterface $c ) { $this->binder = $b; diff --git a/src/Binding/Definition/CallableDefinition.php b/src/Binding/Definition/CallableDefinition.php index 5ceb6d0..737536d 100644 --- a/src/Binding/Definition/CallableDefinition.php +++ b/src/Binding/Definition/CallableDefinition.php @@ -9,8 +9,7 @@ use WPDesk\Init\Binding\Definition; /** @implements Definition<callable> */ class CallableDefinition implements Definition { - /** @var ?string */ - private $hook; + private ?string $hook; /** @var callable */ private $callable; diff --git a/src/Binding/Definition/DefinitionCollection.php b/src/Binding/Definition/DefinitionCollection.php index 99b82ba..b6adf54 100644 --- a/src/Binding/Definition/DefinitionCollection.php +++ b/src/Binding/Definition/DefinitionCollection.php @@ -38,7 +38,7 @@ class DefinitionCollection implements Definition, \IteratorAggregate { yield from $this->defs; } - public function add( Definition $def ) { + public function add( Definition $def ): void { $this->defs[] = $def; } diff --git a/src/Binding/Definition/UnknownDefinition.php b/src/Binding/Definition/UnknownDefinition.php index e238d9b..dd61d8a 100644 --- a/src/Binding/Definition/UnknownDefinition.php +++ b/src/Binding/Definition/UnknownDefinition.php @@ -9,8 +9,7 @@ use WPDesk\Init\Binding\Definition; /** @implements Definition<mixed> */ class UnknownDefinition implements Definition { - /** @var ?string */ - private $hook; + private ?string $hook; /** @var mixed */ private $value; diff --git a/src/Binding/Hookable.php b/src/Binding/Hookable.php index 0ed2900..e8ec977 100644 --- a/src/Binding/Hookable.php +++ b/src/Binding/Hookable.php @@ -1,4 +1,5 @@ <?php +// phpcs:disable declare(strict_types=1); diff --git a/src/Binding/Loader/ArrayDefinitions.php b/src/Binding/Loader/ArrayDefinitions.php index aa30bbe..fd765eb 100644 --- a/src/Binding/Loader/ArrayDefinitions.php +++ b/src/Binding/Loader/ArrayDefinitions.php @@ -10,11 +10,9 @@ use WPDesk\Init\Plugin\Plugin; class ArrayDefinitions implements BindingDefinitions { - /** @var array */ - private $bindings; + private array $bindings; - /** @var DefinitionFactory */ - private $factory; + private DefinitionFactory $factory; public function __construct( array $bindings, ?DefinitionFactory $factory = null ) { $this->bindings = $bindings; @@ -29,10 +27,10 @@ class ArrayDefinitions implements BindingDefinitions { foreach ( $bindings as $key => $value ) { if ( is_array( $value ) ) { if ( isset( $value['handler'] ) ) { - // Single item with handler + // Single item with handler. yield $this->create( $value['handler'], $key, $value ); } else { - // Multiple items + // Multiple items. foreach ( $value as $unit ) { if ( is_array( $unit ) && isset( $unit['handler'] ) ) { yield $this->create( $unit['handler'], $key, $unit ); diff --git a/src/Binding/Loader/CompositeBindingLoader.php b/src/Binding/Loader/CompositeBindingLoader.php index 81f501b..e5755b2 100644 --- a/src/Binding/Loader/CompositeBindingLoader.php +++ b/src/Binding/Loader/CompositeBindingLoader.php @@ -8,7 +8,7 @@ use WPDesk\Init\Plugin\Plugin; class CompositeBindingLoader implements BindingDefinitions { /** @var BindingDefinitionLoader[] */ - private $loaders; + private array $loaders; public function __construct( BindingDefinitions ...$loaders ) { $this->loaders = $loaders; diff --git a/src/Binding/Loader/DebugBindingLoader.php b/src/Binding/Loader/DebugBindingLoader.php index 286237d..67aab6c 100644 --- a/src/Binding/Loader/DebugBindingLoader.php +++ b/src/Binding/Loader/DebugBindingLoader.php @@ -22,7 +22,7 @@ final class DebugBindingLoader implements BindingDefinitions { public function load(): iterable { foreach ( $this->loader->load() as $definition ) { if ( $definition instanceof UnknownDefinition ) { - @trigger_error( + @trigger_error( // phpcs:ignore sprintf( 'Trying to bind unknown value "%1$s". Currently wp-init can handle only simple callables and classes implementing "%2$s" interface', is_string( $definition->value() ) ? $definition->value() : json_encode( $definition->value() ), diff --git a/src/Binding/Loader/FilesystemDefinitions.php b/src/Binding/Loader/FilesystemDefinitions.php index f86c22e..10f0742 100644 --- a/src/Binding/Loader/FilesystemDefinitions.php +++ b/src/Binding/Loader/FilesystemDefinitions.php @@ -9,14 +9,11 @@ use WPDesk\Init\Util\PhpFileLoader; class FilesystemDefinitions implements BindingDefinitions { - /** @var Path */ - private $path; + private Path $path; - /** @var PhpFileLoader */ - private $loader; + private PhpFileLoader $loader; - /** @var DefinitionFactory */ - private $def_factory; + private DefinitionFactory $def_factory; public function __construct( $path, ?PhpFileLoader $loader = null, ?DefinitionFactory $def_factory = null ) { $this->path = new Path( (string) $path ); diff --git a/src/Binding/Loader/OrderedBindingLoader.php b/src/Binding/Loader/OrderedBindingLoader.php index 6ec899a..4aa1c43 100644 --- a/src/Binding/Loader/OrderedBindingLoader.php +++ b/src/Binding/Loader/OrderedBindingLoader.php @@ -19,7 +19,7 @@ final class OrderedBindingLoader implements BindingDefinitions { usort( $definitions, - fn ( $a, $b ) => $a->option( 'priority' ) <=> $b->option( 'priority' ) + fn ( $a, $b ): int => $a->option( 'priority' ) <=> $b->option( 'priority' ) ); yield from array_reverse( $definitions, false ); diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index 69c5b8c..1451a08 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -9,7 +9,7 @@ namespace WPDesk\Init\Configuration; class Configuration implements ReadableConfig, \ArrayAccess { /** @var array<string, mixed> */ - private $config; + private array $config; public function __construct( array $config ) { $this->config = $config; diff --git a/src/DependencyInjection/ContainerBuilder.php b/src/DependencyInjection/ContainerBuilder.php index c147090..3ebbaa6 100644 --- a/src/DependencyInjection/ContainerBuilder.php +++ b/src/DependencyInjection/ContainerBuilder.php @@ -9,8 +9,7 @@ use DI\Definition\Source\DefinitionSource; final class ContainerBuilder { - /** @var DiBuilder */ - private $original_builder; + private DiBuilder $original_builder; public function __construct( DiBuilder $original_builder ) { $this->original_builder = $original_builder; diff --git a/src/Extension/CommonBinding/CustomOrdersTableCompatibility.php b/src/Extension/CommonBinding/CustomOrdersTableCompatibility.php index c5571de..c800f49 100644 --- a/src/Extension/CommonBinding/CustomOrdersTableCompatibility.php +++ b/src/Extension/CommonBinding/CustomOrdersTableCompatibility.php @@ -7,8 +7,7 @@ use WPDesk\Init\Plugin\Plugin; class CustomOrdersTableCompatibility implements Hookable { - /** @var Plugin */ - private $plugin; + private Plugin $plugin; public function __construct( Plugin $plugin ) { $this->plugin = $plugin; @@ -19,7 +18,8 @@ class CustomOrdersTableCompatibility implements Hookable { } public function __invoke(): void { - $features_util_class = '\\' . 'Automattic' . '\\' . 'WooCommerce' . '\\' . 'Utilities' . '\\' . 'FeaturesUtil'; + // Concatenate string to make sure, prefixer will not parse it. + $features_util_class = '\\' . 'Automattic' . '\\' . 'WooCommerce' . '\\' . 'Utilities' . '\\' . 'FeaturesUtil'; //phpcs:ignore Generic.Strings.UnnecessaryStringConcat.Found if ( class_exists( $features_util_class ) ) { $features_util_class::declare_compatibility( 'custom_order_tables', diff --git a/src/Extension/CommonBinding/I18n.php b/src/Extension/CommonBinding/I18n.php index a522173..0f5cd6e 100644 --- a/src/Extension/CommonBinding/I18n.php +++ b/src/Extension/CommonBinding/I18n.php @@ -7,8 +7,7 @@ use WPDesk\Init\Plugin\Plugin; class I18n implements Hookable { - /** @var Plugin */ - private $plugin; + private Plugin $plugin; public function __construct( Plugin $plugin ) { $this->plugin = $plugin; diff --git a/src/Extension/CommonBinding/WPDeskLicenseBridge.php b/src/Extension/CommonBinding/WPDeskLicenseBridge.php index 51d4534..5fbc067 100644 --- a/src/Extension/CommonBinding/WPDeskLicenseBridge.php +++ b/src/Extension/CommonBinding/WPDeskLicenseBridge.php @@ -36,7 +36,7 @@ class WPDeskLicenseBridge implements Hookable { add_action( 'plugins_loaded', $this, -50 ); } - public function __invoke() { + public function __invoke(): void { // Backward compatibility with wp-builder hook. if ( apply_filters( 'wpdesk_can_register_plugin', true, $this->plugin ) === false ) { return; diff --git a/src/Extension/CommonBinding/WPDeskTrackerBridge.php b/src/Extension/CommonBinding/WPDeskTrackerBridge.php index 20660f9..131a9e9 100644 --- a/src/Extension/CommonBinding/WPDeskTrackerBridge.php +++ b/src/Extension/CommonBinding/WPDeskTrackerBridge.php @@ -7,8 +7,7 @@ use WPDesk\Init\Plugin\Plugin; class WPDeskTrackerBridge implements Hookable { - /** @var Plugin */ - private $plugin; + private Plugin $plugin; public function __construct( Plugin $plugin ) { $this->plugin = $plugin; diff --git a/src/Extension/ConfigExtension.php b/src/Extension/ConfigExtension.php index 303c514..0924093 100644 --- a/src/Extension/ConfigExtension.php +++ b/src/Extension/ConfigExtension.php @@ -29,9 +29,7 @@ class ConfigExtension implements Extension { public function build( ContainerBuilder $builder, Plugin $plugin, ReadableConfig $config ): void { $services = array_map( - function ( $service ) use ( $plugin ) { - return (string) ( new Path( $service ) )->absolute( $plugin->get_path() ); - }, + fn( $service ): string => (string) ( new Path( $service ) )->absolute( $plugin->get_path() ), (array) $config->get( 'services', [] ) ); diff --git a/src/Extension/ExtensionsSet.php b/src/Extension/ExtensionsSet.php index 650560e..c5e7fb5 100644 --- a/src/Extension/ExtensionsSet.php +++ b/src/Extension/ExtensionsSet.php @@ -5,12 +5,12 @@ declare( strict_types=1 ); namespace WPDesk\Init\Extension; /** - * @implements \IteratorAggregate<class-string, Extension> + * @implements \IteratorAggregate<class-string<Extension>, Extension> */ class ExtensionsSet implements \IteratorAggregate { /** @var array<class-string<Extension>, Extension> */ - private $extensions = []; + private array $extensions = []; public function __construct( Extension ...$extensions ) { foreach ( $extensions as $extension ) { diff --git a/src/HookDriver/CompositeDriver.php b/src/HookDriver/CompositeDriver.php index ea17e1f..c79522a 100644 --- a/src/HookDriver/CompositeDriver.php +++ b/src/HookDriver/CompositeDriver.php @@ -6,7 +6,7 @@ namespace WPDesk\Init\HookDriver; final class CompositeDriver implements HookDriver { /** @var HookDriver[] */ - private $drivers; + private array $drivers; public function __construct( HookDriver ...$drivers ) { $this->drivers = $drivers; diff --git a/src/HookDriver/GenericDriver.php b/src/HookDriver/GenericDriver.php index 5116742..cfc2d29 100644 --- a/src/HookDriver/GenericDriver.php +++ b/src/HookDriver/GenericDriver.php @@ -8,11 +8,9 @@ use WPDesk\Init\Binding\Loader\BindingDefinitions; class GenericDriver implements HookDriver { - /** @var BindingDefinitions */ - private $definitions; + private BindingDefinitions $definitions; - /** @var Binder */ - private $binder; + private Binder $binder; public function __construct( BindingDefinitions $definitions, Binder $binder ) { $this->definitions = $definitions; @@ -23,7 +21,7 @@ class GenericDriver implements HookDriver { // Load has to be deffered until plugins_loaded because classes may implement or extend interfaces/classes which doesn't exist yet. add_action( 'plugins_loaded', - function () { + function (): void { foreach ( $this->definitions->load() as $definition ) { if ( $definition->hook() === null ) { $this->binder->bind( $definition ); diff --git a/src/HookDriver/Legacy/HooksRegistry.php b/src/HookDriver/Legacy/HooksRegistry.php index 1ef8730..d34499b 100644 --- a/src/HookDriver/Legacy/HooksRegistry.php +++ b/src/HookDriver/Legacy/HooksRegistry.php @@ -11,16 +11,16 @@ use WPDesk\PluginBuilder\Plugin\Hookable; */ final class HooksRegistry implements \IteratorAggregate { - private static $instance; + private static ?HooksRegistry $instance = null; /** @var array<class-string<Hookable>|Hookable> */ - private $callbacks = []; + private array $callbacks = []; - private $container; + private ?ContainerInterface $container = null; private function __construct() {} - public function inject_container( ContainerInterface $c ) { + public function inject_container( ContainerInterface $c ): void { $this->container = $c; } @@ -35,19 +35,13 @@ final class HooksRegistry implements \IteratorAggregate { public function getIterator(): Traversable { return new \ArrayIterator( array_map( - function ( $hookable ) { - if ( is_string( $hookable ) ) { - return $this->container->get( $hookable ); - } - - return $hookable; - }, + fn ( $hookable ) => is_string( $hookable ) ? $this->container->get( $hookable ) : $hookable, $this->callbacks ) ); } - public function add( $hookable ) { + public function add( $hookable ): void { $this->callbacks[] = $hookable; } } diff --git a/src/HookDriver/LegacyDriver.php b/src/HookDriver/LegacyDriver.php index ff0e7e3..33ce6dd 100644 --- a/src/HookDriver/LegacyDriver.php +++ b/src/HookDriver/LegacyDriver.php @@ -8,8 +8,7 @@ use WPDesk\Init\HookDriver\Legacy\HooksRegistry; final class LegacyDriver implements HookDriver { - /** @var ContainerInterface */ - private $container; + private ContainerInterface $container; public function __construct( ContainerInterface $container ) { if ( ! class_exists( \WPDesk_Plugin_Info::class ) ) { diff --git a/src/Init.php b/src/Init.php index 9b8414a..fb88458 100644 --- a/src/Init.php +++ b/src/Init.php @@ -15,6 +15,7 @@ use WPDesk\Init\Extension\ConditionalExtension; final class Init { + /** @var bool */ private static $bootable = true; /** @var Configuration */ @@ -58,7 +59,7 @@ final class Init { } if ( $filename === null ) { - $backtrace = \debug_backtrace( \DEBUG_BACKTRACE_IGNORE_ARGS, 1 ); + $backtrace = \debug_backtrace( \DEBUG_BACKTRACE_IGNORE_ARGS, 1 ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace $filename = $backtrace[0]['file']; } diff --git a/src/Kernel.php b/src/Kernel.php index 6a75c8b..c1b4559 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -33,19 +33,15 @@ use WPDesk\Init\Plugin\Plugin; final class Kernel { /** @var string|null Plugin filename. */ - private $filename; + private ?string $filename; - /** @var Configuration */ - private $config; + private Configuration $config; - /** @var PhpFileLoader */ - private $loader; + private PhpFileLoader $loader; - /** @var HeaderParser */ - private $parser; + private HeaderParser $parser; - /** @var ExtensionsSet */ - private $extensions; + private ExtensionsSet $extensions; private PhpFileDumper $dumper; diff --git a/src/Plugin/Header.php b/src/Plugin/Header.php index 97beba3..91634ec 100644 --- a/src/Plugin/Header.php +++ b/src/Plugin/Header.php @@ -6,8 +6,7 @@ namespace WPDesk\Init\Plugin; final class Header implements \ArrayAccess { - /** @var array */ - private $header_data; + private array $header_data; public function __construct( array $header_data ) { $this->header_data = $header_data; diff --git a/src/Plugin/Plugin.php b/src/Plugin/Plugin.php index 5fd3c2a..19e3e7d 100644 --- a/src/Plugin/Plugin.php +++ b/src/Plugin/Plugin.php @@ -9,57 +9,40 @@ final class Plugin { * Plugin basename. * * Ex: plugin-name/plugin-name.php - * - * @var string */ - private $basename; + private string $basename; /** * Absolute path to the main plugin directory. - * - * @var string */ - private $directory; + private string $directory; /** * Plugin name to display. - * - * @var string */ - private $name; + private string $name; /** * Absolute path to the main plugin file. - * - * @var string */ - private $file; + private string $file; /** * Plugin identifier. - * - * @var string */ - private $slug; + private string $slug; /** * URL to the main plugin directory. - * - * @var string */ - private $url; + private string $url; /** * Plugin version string. - * - * @var string */ - private $version; + private string $version; - /** - * @var Header - */ - private $header; + private Header $header; public function __construct( string $file, Header $header ) { $this->file = $file; diff --git a/src/Util/Path.php b/src/Util/Path.php index a7cf66f..ce4fdef 100644 --- a/src/Util/Path.php +++ b/src/Util/Path.php @@ -6,8 +6,7 @@ namespace WPDesk\Init\Util; final class Path { - /** @var string */ - private $path; + private string $path; public function __construct( string $path ) { $this->path = $path; @@ -19,7 +18,7 @@ final class Path { } public function absolute( ?string $base_path = null ): self { - $base_path = $base_path ?? getcwd(); + $base_path ??= getcwd(); return ( new self( rtrim( $base_path, '/\\' ) . '/' . $this->path ) )->canonical(); } @@ -29,21 +28,21 @@ final class Path { $canonical_parts = []; - // Collapse "." and "..", if possible + // Collapse "." and "..", if possible. foreach ( $parts as $part ) { if ( '.' === $part || '' === $part ) { continue; } // Collapse ".." with the previous part, if one exists - // Don't collapse ".." if the previous part is also ".." + // Don't collapse ".." if the previous part is also "..". if ( '..' === $part && \count( $canonical_parts ) > 0 && '..' !== $canonical_parts[ \count( $canonical_parts ) - 1 ] ) { array_pop( $canonical_parts ); continue; } - // Only add ".." prefixes for relative paths + // Only add ".." prefixes for relative paths. if ( '..' !== $part || '' === $root ) { $canonical_parts[] = $part; } @@ -79,9 +78,7 @@ final class Path { } return array_map( - function ( $file ) { - return ( new self( $file ) )->absolute( $this->path ); - }, + fn( $file ): Path => ( new self( $file ) )->absolute( $this->path ), array_values( array_diff( scandir( $this->path ), diff --git a/src/Util/PhpFileDumper.php b/src/Util/PhpFileDumper.php index 8bc45f9..7f777de 100644 --- a/src/Util/PhpFileDumper.php +++ b/src/Util/PhpFileDumper.php @@ -1,4 +1,5 @@ <?php +// phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged declare( strict_types=1 ); namespace WPDesk\Init\Util; @@ -11,7 +12,7 @@ class PhpFileDumper { $content = '<?php' . PHP_EOL . PHP_EOL; $content .= 'declare(strict_types=1);' . PHP_EOL . PHP_EOL; - $content .= 'return ' . var_export( $config, true ) . ';' . PHP_EOL; + $content .= 'return ' . var_export( $config, true ) . ';' . PHP_EOL; // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export $this->writeFileAtomic( $filename, $content ); } diff --git a/src/di-functions.php b/src/di-functions.php index 993a5b3..60472ce 100644 --- a/src/di-functions.php +++ b/src/di-functions.php @@ -1,4 +1,5 @@ <?php +// phpcs:disable WordPress.NamingConventions.ValidVariableName /** * This is a vendored set of php-di/php-di helper functions. We place it directly in our library to improve experience when using DI along with code scoping, which defy composer's file autoloading. * diff --git a/src/platform_check.php b/src/platform_check.php index 5219843..365bf21 100644 --- a/src/platform_check.php +++ b/src/platform_check.php @@ -5,7 +5,7 @@ if ( ! ( PHP_VERSION_ID >= 70400 ) ) { function () { printf( '<div class="notice notice-error"><p>%s</p></div>', - __( 'The plugin cannot run on PHP versions older than 7.4. Please, contact your host and ask them to upgrade.', 'wp-init' ) + esc_html__( 'The plugin cannot run on PHP versions older than 7.4. Please, contact your host and ask them to upgrade.', 'wp-init' ) ); } ); diff --git a/tests/DefaultHeaderParserTest.php b/tests/DefaultHeaderParserTest.php index f3f2a40..372b66d 100644 --- a/tests/DefaultHeaderParserTest.php +++ b/tests/DefaultHeaderParserTest.php @@ -8,7 +8,7 @@ use WPDesk\Init\Plugin\DefaultHeaderParser; class DefaultHeaderParserTest extends TestCase { /** @dataProvider provider */ - public function test_should_parse_plugin_data_from_file( $name, string $content, array $expected ): void { + public function test_should_parse_plugin_data_from_file( string $name, string $content, array $expected ): void { $file = $this->createTempFile($name, $content); $data = new DefaultHeaderParser(); @@ -18,18 +18,18 @@ class DefaultHeaderParserTest extends TestCase { public function provider(): iterable { yield [ 'first.php', -<<<PHP +<<<PHP_WRAP <?php /** * Plugin Name: Example plugin */ -PHP, +PHP_WRAP, [ 'Name' => 'Example plugin' ], ]; yield [ 'second.php', -<<<PHP +<<<PHP_WRAP <?php /** * Plugin Name: ShopMagic for WooCommerce @@ -46,7 +46,8 @@ PHP, * WC tested up to: 7.2 * Requires PHP: 7.2 */ -PHP, +PHP_WRAP +, [ 'Name' => 'ShopMagic for WooCommerce', 'PluginURI' => 'https://shopmagic.app/', diff --git a/tests/Dumper/PhpFileDumperTest.php b/tests/Dumper/PhpFileDumperTest.php index 8446003..56cfe58 100644 --- a/tests/Dumper/PhpFileDumperTest.php +++ b/tests/Dumper/PhpFileDumperTest.php @@ -7,7 +7,7 @@ use WPDesk\Init\Util\PhpFileDumper; class PhpFileDumperTest extends \WPDesk\Init\Tests\TestCase { - public function test_dump_php_file() { + public function test_dump_php_file(): void { $dir = $this->initTempPlugin(); $dumper = new PhpFileDumper(); $dumper->dump( [ 'foo' => 'bar' ], $dir . '/dump.php' ); diff --git a/tests/HookDriver/GenericDriverTest.php b/tests/HookDriver/GenericDriverTest.php index ce92478..c1e1f1a 100644 --- a/tests/HookDriver/GenericDriverTest.php +++ b/tests/HookDriver/GenericDriverTest.php @@ -22,7 +22,7 @@ class GenericDriverTest extends TestCase { public function bind( Definition $def ): void { } }), - function ( $binder ) { + function ( $binder ): void { $this->assertTrue( $binder->is_bound() ); } ]; diff --git a/tests/Loader/PhpFileLoaderTest.php b/tests/Loader/PhpFileLoaderTest.php index b996b7e..3a5ac97 100644 --- a/tests/Loader/PhpFileLoaderTest.php +++ b/tests/Loader/PhpFileLoaderTest.php @@ -7,7 +7,7 @@ use WPDesk\Init\Util\PhpFileLoader; class PhpFileLoaderTest extends \WPDesk\Init\Tests\TestCase { - public function test_load_php_file() { + public function test_load_php_file(): void { $loader = new PhpFileLoader(); $resource = __DIR__ . '/../Fixtures/load.php'; $data = $loader->load( $resource ); diff --git a/tests/generated/plugin.php b/tests/generated/plugin.php index a0057af..0dae23d 100644 --- a/tests/generated/plugin.php +++ b/tests/generated/plugin.php @@ -2,5 +2,4 @@ declare(strict_types=1); -return array ( -); +return []; -- GitLab