Skip to content

Commit

Permalink
Fix psalm errors
Browse files Browse the repository at this point in the history
  • Loading branch information
inpsyde-maticluznar committed Jul 25, 2024
1 parent ee95bca commit 2a488cf
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/AssetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -90,7 +90,7 @@ public static function create(array $config): Asset
}

$inFooter = $config['inFooter'] ?? true;
$inFooter
$inFooter === true
? $asset->isInFooter()
: $asset->isInHeader();

Expand Down
10 changes: 6 additions & 4 deletions src/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)]);
}
}
Expand Down Expand Up @@ -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 [];
}

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/Caching/IgnoreSitegroundCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ public function apply(array $handles): void
* Ignore Javascript
*/
add_filter('sgo_js_minify_exclude', static function (array $scripts) use ($handles) {

Check warning on line 26 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L26

Added line #L26 was not covered by tests
assert(is_array($handles[Script::class]));
return array_merge($scripts, $handles[Script::class]);
});

Check warning on line 29 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L28-L29

Added lines #L28 - L29 were not covered by tests

add_filter(
'sgo_javascript_combine_exclude',
static function (array $scripts) use ($handles) {

Check warning on line 33 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L31-L33

Added lines #L31 - L33 were not covered by tests
assert(is_array($handles[Script::class]));
return array_merge($scripts, $handles[Script::class]);
}
);

Check warning on line 37 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L35-L37

Added lines #L35 - L37 were not covered by tests
Expand All @@ -38,9 +40,11 @@ static function (array $scripts) use ($handles) {
* Ignore Styles
*/
add_filter('sgo_css_minify_exclude', static function (array $styles) use ($handles) {

Check warning on line 42 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L42

Added line #L42 was not covered by tests
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) {

Check warning on line 46 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L44-L46

Added lines #L44 - L46 were not covered by tests
assert(is_array($handles[Style::class]));
return array_merge($styles, $handles[Style::class]);
});

Check warning on line 49 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L48-L49

Added lines #L48 - L49 were not covered by tests
}
Expand Down
4 changes: 2 additions & 2 deletions src/Caching/IgnoreW3TotalCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check warning on line 30 in src/Caching/IgnoreW3TotalCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreW3TotalCache.php#L27-L30

Added lines #L27 - L30 were not covered by tests
}
}
Expand All @@ -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;

Check warning on line 42 in src/Caching/IgnoreW3TotalCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreW3TotalCache.php#L39-L42

Added lines #L39 - L42 were not covered by tests
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/Loader/AbstractWebpackLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Check warning on line 74 in src/Loader/AbstractWebpackLoader.php

View check run for this annotation

Codecov / codecov/patch

src/Loader/AbstractWebpackLoader.php#L74

Added line #L74 was not covered by tests
}

$data = json_decode($json, true);
$errorCode = json_last_error();
if (0 < $errorCode) {
throw new InvalidResourceException(
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/OutputFilter/AsyncStyleOutputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/OutputFilter/InlineAssetOutputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __invoke(string $html, Asset $asset): string
}

$content = @file_get_contents($filePath);
if (! $content) {
if ($content === false) {
return $html;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Script.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Inpsyde\Assets;

use Inpsyde\Assets\Handler\AssetHandler;
use Inpsyde\Assets\Handler\ScriptHandler;

class Script extends BaseAsset implements Asset
Expand Down Expand Up @@ -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);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Inpsyde\Assets;

use Inpsyde\Assets\Handler\AssetHandler;
use Inpsyde\Assets\Handler\StyleHandler;
use Inpsyde\Assets\OutputFilter\AsyncStyleOutputFilter;

Expand Down Expand Up @@ -73,7 +72,7 @@ public function inlineStyles(): ?array
*/
public function withInlineStyles(string $inline): Style
{
if (!$this->inlineStyles) {
if (!is_array($this->inlineStyles)) {
$this->inlineStyles = [];
}

Expand Down
14 changes: 7 additions & 7 deletions src/Util/AssetPathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check warning on line 45 in src/Util/AssetPathResolver.php

View check run for this annotation

Codecov / codecov/patch

src/Util/AssetPathResolver.php#L44-L45

Added lines #L44 - L45 were not covered by tests
$abspathParent = dirname($abspath);

$relativeVendorPath = null;
Expand All @@ -52,12 +52,12 @@ public static function resolveForVendorUrl(string $normalizedUrl): ?string
$relativeVendorPath = substr($normalizedUrl, strlen($abspathParent));
}

if (!$relativeVendorPath) {
if ($relativeVendorPath !== null && $relativeVendorPath !== '') {

Check warning on line 55 in src/Util/AssetPathResolver.php

View check run for this annotation

Codecov / codecov/patch

src/Util/AssetPathResolver.php#L55

Added line #L55 was not covered by tests
// vendor is not inside ABSPATH, nor inside its parent
return null;
}

$relativeVendorPath = trim($relativeVendorPath, '/');
$relativeVendorPath = trim((string)$relativeVendorPath, '/');

Check warning on line 60 in src/Util/AssetPathResolver.php

View check run for this annotation

Codecov / codecov/patch

src/Util/AssetPathResolver.php#L60

Added line #L60 was not covered by tests

// problematic, as said above: we are assuming vendor URL, but this assumption isn't safe
$vendorUrl = network_site_url("/{$relativeVendorPath}");
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 !== ''

Check warning on line 117 in src/Util/AssetPathResolver.php

View check run for this annotation

Codecov / codecov/patch

src/Util/AssetPathResolver.php#L117

Added line #L117 was not covered by tests
? trailingslashit(wp_normalize_path($basePath)) . trim($relativePluginUrl, '/')
: null;
}
Expand Down

0 comments on commit 2a488cf

Please sign in to comment.