diff --git a/src/AssetFactory.php b/src/AssetFactory.php index 1135f98..c7ba657 100644 --- a/src/AssetFactory.php +++ b/src/AssetFactory.php @@ -14,8 +14,8 @@ namespace Inpsyde\Assets; use Inpsyde\Assets\Exception\InvalidArgumentException; -use Inpsyde\Assets\Loader\PhpFileLoader; use Inpsyde\Assets\Loader\ArrayLoader; +use Inpsyde\Assets\Loader\PhpFileLoader; /** * Class AssetFactory @@ -90,7 +90,7 @@ public static function create(array $config): Asset } $inFooter = $config['inFooter'] ?? true; - $inFooter + $inFooter === true ? $asset->isInFooter() : $asset->isInHeader(); diff --git a/src/AssetManager.php b/src/AssetManager.php index 44ebc0a..84c161d 100644 --- a/src/AssetManager.php +++ b/src/AssetManager.php @@ -19,7 +19,6 @@ use Inpsyde\Assets\Handler\ScriptHandler; use Inpsyde\Assets\Handler\StyleHandler; use Inpsyde\Assets\Util\AssetHookResolver; -use Inpsyde\Assets\Asset; final class AssetManager { @@ -114,7 +113,7 @@ public function register(Asset $asset, Asset ...$assets): AssetManager foreach ($assets as $asset) { $handle = $asset->handle(); - if ($handle) { + if ($handle !== '') { $this->assets->attach($asset, [$handle, get_class($asset)]); } } @@ -258,7 +257,7 @@ private function loopCurrentHookAssets(string $currentHook, bool $process): arra /** @var int|null $locationId */ $locationId = Asset::HOOK_TO_LOCATION[$currentHook] ?? null; - if (!$locationId) { + if (is_null($locationId)) { return []; } @@ -314,7 +313,10 @@ private function ensureSetup(): void * * @psalm-suppress PossiblyNullArgument */ - if (!$lastHook && did_action($lastHook) && !doing_action($lastHook)) { + if ( + (is_null($lastHook) || $lastHook === '') && + did_action($lastHook) && !doing_action($lastHook) + ) { $this->assets = new \SplObjectStorage(); return; diff --git a/src/Caching/IgnoreSitegroundCache.php b/src/Caching/IgnoreSitegroundCache.php index ed06be1..2b63029 100644 --- a/src/Caching/IgnoreSitegroundCache.php +++ b/src/Caching/IgnoreSitegroundCache.php @@ -24,12 +24,14 @@ public function apply(array $handles): void * Ignore Javascript */ add_filter('sgo_js_minify_exclude', static function (array $scripts) use ($handles) { + assert(is_array($handles[Script::class])); return array_merge($scripts, $handles[Script::class]); }); add_filter( 'sgo_javascript_combine_exclude', static function (array $scripts) use ($handles) { + assert(is_array($handles[Script::class])); return array_merge($scripts, $handles[Script::class]); } ); @@ -38,9 +40,11 @@ static function (array $scripts) use ($handles) { * Ignore Styles */ add_filter('sgo_css_minify_exclude', static function (array $styles) use ($handles) { + assert(is_array($handles[Style::class])); return array_merge($styles, $handles[Style::class]); }); add_filter('sgo_css_combine_exclude', static function (array $styles) use ($handles) { + assert(is_array($handles[Style::class])); return array_merge($styles, $handles[Style::class]); }); } diff --git a/src/Caching/IgnoreW3TotalCache.php b/src/Caching/IgnoreW3TotalCache.php index 25073a3..186f2a8 100644 --- a/src/Caching/IgnoreW3TotalCache.php +++ b/src/Caching/IgnoreW3TotalCache.php @@ -26,7 +26,7 @@ public function apply(array $handles): void */ add_filter('w3tc_minify_js_do_tag_minification', static function (bool $doMinification, string $scriptTag) use ($handles) { foreach ($handles[Script::class] as $handle) { - if (strpos($scriptTag, $handle) !== false) { + if (strpos($scriptTag, (string)$handle) !== false) { return false; } } @@ -38,7 +38,7 @@ public function apply(array $handles): void */ add_filter('w3tc_minify_css_do_tag_minification', static function (bool $doMinification, string $scriptTag) use ($handles) { foreach ($handles[Style::class] as $handle) { - if (strpos($scriptTag, $handle) !== false) { + if (strpos($scriptTag, (string)$handle) !== false) { return false; } } diff --git a/src/Loader/AbstractWebpackLoader.php b/src/Loader/AbstractWebpackLoader.php index a8f9655..840ad76 100644 --- a/src/Loader/AbstractWebpackLoader.php +++ b/src/Loader/AbstractWebpackLoader.php @@ -68,10 +68,13 @@ public function load($resource): array ) ); } + $json = @file_get_contents($resource); - $data = @file_get_contents($resource) - ?: ''; // phpcs:ignore - $data = json_decode($data, true); + if ($json === false) { + $json = ''; + } + + $data = json_decode($json, true); $errorCode = json_last_error(); if (0 < $errorCode) { throw new InvalidResourceException( @@ -183,23 +186,23 @@ protected function sanitizeFileName(string $file): string */ protected function resolveLocation(string $fileName): int { - if (stristr($fileName, '-backend')) { + if (stristr($fileName, '-backend') !== false) { return Asset::BACKEND; } - if (stristr($fileName, '-block')) { + if (stristr($fileName, '-block') !== false) { return Asset::BLOCK_EDITOR_ASSETS; } - if (stristr($fileName, '-login')) { + if (stristr($fileName, '-login') !== false) { return Asset::LOGIN; } - if (stristr($fileName, '-customizer-preview')) { + if (stristr($fileName, '-customizer-preview') !== false) { return Asset::CUSTOMIZER_PREVIEW; } - if (stristr($fileName, '-customizer')) { + if (stristr($fileName, '-customizer') !== false) { return Asset::CUSTOMIZER; } diff --git a/src/OutputFilter/AsyncStyleOutputFilter.php b/src/OutputFilter/AsyncStyleOutputFilter.php index 98cc1ef..753d846 100644 --- a/src/OutputFilter/AsyncStyleOutputFilter.php +++ b/src/OutputFilter/AsyncStyleOutputFilter.php @@ -39,7 +39,7 @@ public function __invoke(string $html, Asset $asset): string { $url = $asset->url(); $version = $asset->version(); - if ($version) { + if ($version !== null && $version !== '') { $url = add_query_arg('ver', $version, $url); } diff --git a/src/OutputFilter/InlineAssetOutputFilter.php b/src/OutputFilter/InlineAssetOutputFilter.php index 63bffd7..edc85e8 100644 --- a/src/OutputFilter/InlineAssetOutputFilter.php +++ b/src/OutputFilter/InlineAssetOutputFilter.php @@ -35,7 +35,7 @@ public function __invoke(string $html, Asset $asset): string } $content = @file_get_contents($filePath); - if (! $content) { + if ($content === false) { return $html; } diff --git a/src/Script.php b/src/Script.php index 44a8b8b..168d9b4 100644 --- a/src/Script.php +++ b/src/Script.php @@ -13,7 +13,6 @@ namespace Inpsyde\Assets; -use Inpsyde\Assets\Handler\AssetHandler; use Inpsyde\Assets\Handler\ScriptHandler; class Script extends BaseAsset implements Asset @@ -260,7 +259,7 @@ protected function resolveDependencyExtractionPlugin(): bool $version = $data['version'] ?? null; $this->withDependencies(...$dependencies); - if (!$this->version && $version) { + if (is_null($this->version) && !is_null($version)) { $this->withVersion($version); } diff --git a/src/Style.php b/src/Style.php index 82ee727..e9f888b 100644 --- a/src/Style.php +++ b/src/Style.php @@ -13,7 +13,6 @@ namespace Inpsyde\Assets; -use Inpsyde\Assets\Handler\AssetHandler; use Inpsyde\Assets\Handler\StyleHandler; use Inpsyde\Assets\OutputFilter\AsyncStyleOutputFilter; @@ -73,7 +72,7 @@ public function inlineStyles(): ?array */ public function withInlineStyles(string $inline): Style { - if (!$this->inlineStyles) { + if (!is_array($this->inlineStyles)) { $this->inlineStyles = []; } diff --git a/src/Util/AssetPathResolver.php b/src/Util/AssetPathResolver.php index 7b49551..1b40f9a 100644 --- a/src/Util/AssetPathResolver.php +++ b/src/Util/AssetPathResolver.php @@ -41,8 +41,8 @@ public static function resolveForVendorUrl(string $normalizedUrl): ?string // Now let's see if it's inside vendor. // This is problematic, this is why vendor assets should be "published". - $fullVendorPath = wp_normalize_path(realpath(__DIR__ . '/../../../')); - $abspath = wp_normalize_path(ABSPATH); + $fullVendorPath = wp_normalize_path((string)realpath(__DIR__ . '/../../../')); + $abspath = wp_normalize_path((string)ABSPATH); $abspathParent = dirname($abspath); $relativeVendorPath = null; @@ -52,12 +52,12 @@ public static function resolveForVendorUrl(string $normalizedUrl): ?string $relativeVendorPath = substr($normalizedUrl, strlen($abspathParent)); } - if (!$relativeVendorPath) { + if ($relativeVendorPath !== null && $relativeVendorPath !== '') { // vendor is not inside ABSPATH, nor inside its parent return null; } - $relativeVendorPath = trim($relativeVendorPath, '/'); + $relativeVendorPath = trim((string)$relativeVendorPath, '/'); // problematic, as said above: we are assuming vendor URL, but this assumption isn't safe $vendorUrl = network_site_url("/{$relativeVendorPath}"); @@ -90,7 +90,7 @@ public static function resolveForThemeUrl(string $normalizedUrl): ?string $relativeThemeUrl = substr($normalizedUrl, strlen($themeUrl)); } - return $relativeThemeUrl + return $relativeThemeUrl !== null && $relativeThemeUrl !== '' ? trailingslashit($base) . trim($relativeThemeUrl, '/') : null; } @@ -113,8 +113,8 @@ public static function resolveForPluginUrl(string $normalizedUrl): ?string $basePath = WPMU_PLUGIN_DIR; $relativePluginUrl = substr($normalizedUrl, strlen($muPluginsUrl)); } - - return $relativePluginUrl + assert(is_string($basePath)); + return $relativePluginUrl !== null && $relativePluginUrl !== '' ? trailingslashit(wp_normalize_path($basePath)) . trim($relativePluginUrl, '/') : null; }