Skip to content

Commit

Permalink
Update phpstan/phpstan requirement from ^1.11 to ^1.12 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot[bot] authored Sep 18, 2024
1 parent 72d50d4 commit fb3677c
Show file tree
Hide file tree
Showing 21 changed files with 337 additions and 114 deletions.
4 changes: 3 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
"wongjn.php-sniffer",
"wordpresstoolbox.wordpress-toolbox",
"SanderRonde.phpstan-vscode",
"xdebug.php-debug"
"xdebug.php-debug",
"christian-kohler.path-intellisense",
"VisualStudioExptTeam.vscodeintellicode"
]
}
}
Expand Down
96 changes: 16 additions & 80 deletions app/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
use Codex\Contracts\Activatable;
use Codex\Contracts\Bootable;
use Codex\Contracts\Deactivatable;
use Codex\Contracts\Enqueueable;
use Codex\Contracts\Extendable;
use Codex\Contracts\HasAdminScripts;
use Codex\Contracts\HasPublicScripts;
use Codex\Contracts\Hookable;
use Codex\Core\App;
use Codex\Core\Config;
use Codex\Foundation\Assets\Assets;
use Codex\Foundation\Assets\Enqueue;
use Codex\Foundation\Assets\Script;
use Codex\Foundation\Assets\Style;
use Codex\Foundation\Blocks;
use Codex\Foundation\Hooks\Hook;
use Codex\Foundation\Settings\Registry;
use InvalidArgumentException;
Expand All @@ -32,7 +31,6 @@
use function dirname;
use function is_dir;
use function is_file;
use function is_iterable;
use function is_string;
use function is_subclass_of;

Expand Down Expand Up @@ -142,7 +140,12 @@ public function boot(): void
$blocksPath = wp_normalize_path($blocksPath);

if (is_dir($blocksPath)) {
$this->hook->addAction('init', fn () => $this->registerBlocks($blocksPath));
$blocks = new Blocks($blocksPath);

/**
* Register the blocks found in the specificed blocks directory.
*/
$this->hook->addAction('init', [$blocks, 'register'], 10, 1, ['id' => 'app.blocks.register']);
}
}

Expand Down Expand Up @@ -192,10 +195,13 @@ private function bootInstances(): void
}

if ($this->container->has('enqueue')) {
/** @var Enqueue $enqueue */
$enqueue = $this->container->get('enqueue');

$this->bootEnqueueables($instance, $enqueue);
if ($enqueue instanceof Enqueue && ($instance instanceof HasAdminScripts || $instance instanceof HasPublicScripts)) {
$assets = new Assets($enqueue);
$assets->hook($this->hook);
$assets->enqueue($instance);
}
}

if (! ($instance instanceof Bootable)) {
Expand All @@ -208,6 +214,7 @@ private function bootInstances(): void

private function registerCoreServices(): void
{
$this->pimple['hook'] = $this->hook;
$this->pimple['config'] = function (): Config {
$config = [];
$configDir = dirname($this->pluginFilePath) . '/inc/config';
Expand All @@ -234,6 +241,8 @@ private function registerCoreServices(): void

return new Config($config);
};

$this->pimple['app.plugin_file_path'] = $this->pluginFilePath;
$this->pimple['app'] = static function (PimpleContainer $container): App {
/** @var Config $config */
$config = $container['config'];
Expand All @@ -249,78 +258,5 @@ private function registerCoreServices(): void

return new App($name, $settings);
};
$this->pimple['app.plugin_file_path'] = $this->pluginFilePath;
}

private function bootEnqueueables(object $instance, Enqueue $enqueue): void
{
if ($instance instanceof HasAdminScripts) {
$this->hook->addAction(
'admin_enqueue_scripts',
function (string $admin) use ($instance, $enqueue): void {
$scripts = $instance->getAdminScripts($admin);

if (! is_iterable($scripts)) {
return;
}

$this->enqueueScripts($scripts, $enqueue);
},
12,
);
}

if (! ($instance instanceof HasPublicScripts)) {
return;
}

$this->hook->addAction(
'wp_enqueue_scripts',
function () use ($instance, $enqueue): void {
$scripts = $instance->getPublicScripts();

if (! is_iterable($scripts)) {
return;
}

$this->enqueueScripts($scripts, $enqueue);
},
12,
);
}

/** @param iterable<Enqueueable> $assets */
private function enqueueScripts(iterable $assets, Enqueue $enqueue): void
{
foreach ($assets as $asset) {
if ($asset instanceof Script) {
$enqueue->addScripts($asset);
}

if (! ($asset instanceof Style)) {
continue;
}

$enqueue->addStyles($asset);
}

$enqueue->scripts();
$enqueue->styles();
}

private function registerBlocks(string $blocksPath): void
{
$blocks = new RecursiveDirectoryIterator(
$blocksPath,
RecursiveDirectoryIterator::SKIP_DOTS,
);

foreach ($blocks as $block) {
if (! ($block instanceof SplFileInfo) || ! $block->isDir()) {
continue;
}

register_block_type($block->getRealPath());
}
}
}
85 changes: 85 additions & 0 deletions app/Foundation/Assets/Assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Codex\Foundation\Assets;

use Codex\Contracts\Enqueueable;
use Codex\Contracts\HasAdminScripts;
use Codex\Contracts\HasPublicScripts;
use Codex\Contracts\Hookable;
use Codex\Foundation\Hooks\Hook;

use function is_iterable;

class Assets implements Hookable
{
private Hook $hook;

private Enqueue $enqueue;

public function __construct(Enqueue $enqueue)
{
$this->enqueue = $enqueue;
}

public function hook(Hook $hook): void
{
$this->hook = $hook;
}

/** @param HasAdminScripts|HasPublicScripts $instance */
public function enqueue($instance): void
{
if ($instance instanceof HasAdminScripts) {
$this->hook->addAction(
'admin_enqueue_scripts',
function (string $admin) use ($instance): void {
$assets = $instance->getAdminScripts($admin);

if (! is_iterable($assets)) {
return;
}

$this->enqueueScripts($assets);
},
12,
);

return;
}

$this->hook->addAction(
'wp_enqueue_scripts',
function () use ($instance): void {
$assets = $instance->getPublicScripts();

if (! is_iterable($assets)) {
return;
}

$this->enqueueScripts($assets);
},
12,
);
}

/** @param iterable<Enqueueable> $assets */
private function enqueueScripts(iterable $assets): void
{
foreach ($assets as $asset) {
if ($asset instanceof Script) {
$this->enqueue->addScripts($asset);
}

if (! ($asset instanceof Style)) {
continue;
}

$this->enqueue->addStyles($asset);
}

$this->enqueue->scripts();
$this->enqueue->styles();
}
}
42 changes: 42 additions & 0 deletions app/Foundation/Blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Codex\Foundation;

use InvalidArgumentException;
use RecursiveDirectoryIterator;
use SplFileInfo;

use function is_dir;

class Blocks
{
private string $blocksPath;

/** @param string $blocksPath The path where all the blocks are located. */
public function __construct(string $blocksPath)
{
$this->blocksPath = $blocksPath;

if (! is_dir($this->blocksPath)) {
throw new InvalidArgumentException('The blocks path is not a directory.');
}
}

public function register(): void
{
$blocks = new RecursiveDirectoryIterator(
$this->blocksPath,
RecursiveDirectoryIterator::SKIP_DOTS,
);

foreach ($blocks as $block) {
if (! ($block instanceof SplFileInfo) || ! $block->isDir()) {
continue;
}

register_block_type($block->getRealPath());
}
}
}
60 changes: 52 additions & 8 deletions app/Foundation/Hooks/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function addFilter(
*/
public function removeAction(string $tag, $ref, int $priority = 10): void
{
$callback = is_string($ref) ? $this->getCallbackFromId($ref) : $ref;
$callback = is_string($ref) ? $this->getCallback($ref) : $ref;

if (! is_callable($callback)) {
return;
Expand All @@ -131,7 +131,7 @@ public function removeAction(string $tag, $ref, int $priority = 10): void
*/
public function removeFilter(string $tag, $ref, int $priority = 10): void
{
$callback = is_string($ref) ? $this->getCallbackFromId($ref) : $ref;
$callback = $this->getCallback($ref);

if (! is_callable($callback)) {
return;
Expand All @@ -140,6 +140,44 @@ public function removeFilter(string $tag, $ref, int $priority = 10): void
remove_filter($tag, $callback, $priority);
}

/**
* Whether the action hook has the specified callback.
*
* @param string $tag The name of the action hook to remove the callback from.
* @param string|callable $ref The callback or ref id to remove from the filter hook.
*
* @return bool|int If registered, it returns the priority of the callback. Otherwise, it returns false.
*/
public function hasAction(string $tag, $ref)
{
$callback = $this->getCallback($ref);

if (! is_callable($callback)) {
return false;
}

return has_action($tag, $callback);
}

/**
* Whether the filter hook has the specified callback.
*
* @param string $tag The name of the filter hook to remove the callback from.
* @param string|callable $ref The callback or ref id to remove from the filter hook.
*
* @return bool|int If registered, it returns the priority of the callback. Otherwise, it returns false.
*/
public function hasFilter(string $tag, $ref)
{
$callback = $this->getCallback($ref);

if (! is_callable($callback)) {
return false;
}

return has_filter($tag, $callback);
}

/**
* Remove all actions and filters from WordPress.
*/
Expand Down Expand Up @@ -239,15 +277,21 @@ private function getNativeId(callable $callback): string
return spl_object_hash(Closure::fromCallable($callback));
}

/** @param string $id The callback or ref to remove from the action hook. */
private function getCallbackFromId(string $id): ?callable
/** @param string|callable $ref The callback or ref to remove from the action hook. */
private function getCallback($ref): ?callable
{
if (isset($this->aliases[$id])) {
return $this->refs[$this->aliases[$id]]['callback'];
if (is_string($ref)) {
if (isset($this->aliases[$ref])) {
return $this->refs[$this->aliases[$ref]]['callback'];
}

if (isset($this->refs[$ref])) {
return $this->refs[$ref]['callback'];
}
}

if (isset($this->refs[$id])) {
return $this->refs[$id]['callback'];
if (is_callable($ref)) {
return $ref;
}

return null;
Expand Down
Loading

0 comments on commit fb3677c

Please sign in to comment.