diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.yml index 7151e22..36ddd06 100644 --- a/.github/workflows/ci_cd.yml +++ b/.github/workflows/ci_cd.yml @@ -93,6 +93,7 @@ jobs: docker run -v $(pwd):/app docker.pkg.github.com/mattvb91/${{ env.REPO_NAME }}/composer:head composer install docker run -v $(pwd):/app docker.pkg.github.com/mattvb91/${{ env.REPO_NAME }}/composer:head composer phpstan docker run -v $(pwd):/app docker.pkg.github.com/mattvb91/${{ env.REPO_NAME }}/composer:head composer codesniffer + docker run -v $(pwd):/app docker.pkg.github.com/mattvb91/${{ env.REPO_NAME }}/composer:head composer rector docker run --network="caddy-network" -e XDEBUG_MODE=coverage -v $(pwd):/app docker.pkg.github.com/mattvb91/${{ env.REPO_NAME }}/composer:head composer phpunit env: REPO_NAME: ${{ github.event.repository.name }} diff --git a/composer.json b/composer.json index dc89303..b4b159e 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,9 @@ "phpunit": "phpunit --testdox --coverage-clover=coverage.xml", "phpstan": "phpstan analyse", "codesniffer": "phpcs ./src ./tests/**/*.php --standard=./codesniffer.xml -p", - "codefixer": "phpcbf ./src ./tests/**/*.php --standard=./codesniffer.xml" + "codefixer": "phpcbf ./src ./tests/**/*.php --standard=./codesniffer.xml", + "rector": "rector --dry-run", + "rector-fix": "rector" }, "autoload": { "psr-4": { @@ -30,6 +32,7 @@ "phpunit/phpunit": "^9.5", "dms/phpunit-arraysubset-asserts": "^0.4.0", "phpstan/phpstan": "^1.10", - "squizlabs/php_codesniffer": "^3.7" + "squizlabs/php_codesniffer": "^3.7", + "rector/rector": "^0.18.13" } } diff --git a/docker/composer/Dockerfile b/docker/composer/Dockerfile index 3f7586d..27386e1 100644 --- a/docker/composer/Dockerfile +++ b/docker/composer/Dockerfile @@ -1,7 +1,7 @@ -FROM composer:2.6.5 +FROM composer:2.6.6 RUN apk --update --no-cache add autoconf g++ make linux-headers \ - && pecl install -f xdebug-3.2.2 \ + && pecl install -f xdebug-3.3 \ && docker-php-ext-enable xdebug \ && apk del --purge autoconf g++ make linux-headers diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..2c9c32e --- /dev/null +++ b/rector.php @@ -0,0 +1,23 @@ +paths([ + __DIR__ . '/src', + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + + // define sets of rules + $rectorConfig->sets([ + \Rector\Set\ValueObject\SetList::DEAD_CODE, + \Rector\Set\ValueObject\SetList::CODE_QUALITY, + \Rector\Set\ValueObject\SetList::TYPE_DECLARATION, + ]); +}; diff --git a/src/Caddy.php b/src/Caddy.php index 69299cc..ed69cc2 100644 --- a/src/Caddy.php +++ b/src/Caddy.php @@ -217,10 +217,10 @@ public function toArray(): array $config['logging'] = $this->logging->toArray(); } - if (count($this->apps)) { + if ($this->apps !== []) { $apps = []; - array_map(static function (App $app, string $appNamespace) use (&$apps) { + array_map(static function (App $app, string $appNamespace) use (&$apps): void { $apps[$appNamespace] = $app->toArray(); }, $this->apps, array_keys($this->apps)); @@ -231,13 +231,11 @@ public function toArray(): array } /** - * @param string $hostIdentifier - * @return void * @throws \Exception */ protected function buildHostsCache(string $hostIdentifier): void { - if (!key_exists($hostIdentifier, $this->hostsCache)) { + if (!array_key_exists($hostIdentifier, $this->hostsCache)) { //Find the host so we can get its path $hostPath = null; diff --git a/src/Config/Apps/Cache.php b/src/Config/Apps/Cache.php index 73a89d4..3709997 100644 --- a/src/Config/Apps/Cache.php +++ b/src/Config/Apps/Cache.php @@ -116,7 +116,7 @@ public function toArray(): array } if (isset($this->cacheKeys)) { - $array['cache_keys'] = array_map(static function (Key $key) { + $array['cache_keys'] = array_map(static function (Key $key): array { return [$key->getPattern() => $key->toArray()]; }, $this->cacheKeys)[0]; //TODO there has to be a better way than [0] access to get this level } diff --git a/src/Config/Apps/Cache/Cdn.php b/src/Config/Apps/Cache/Cdn.php index 3a90a95..3e2bb2d 100644 --- a/src/Config/Apps/Cache/Cdn.php +++ b/src/Config/Apps/Cache/Cdn.php @@ -9,27 +9,17 @@ class Cdn implements Arrayable private bool $dynamic; private string $strategy; - /** - * @param bool $_dynamic - * @param string $_strategy - */ public function __construct(bool $_dynamic = true, string $_strategy = 'hard') { $this->dynamic = $_dynamic; $this->strategy = $_strategy; } - /** - * @param bool $dynamic - */ public function setDynamic(bool $dynamic): void { $this->dynamic = $dynamic; } - /** - * @param string $strategy - */ public function setStrategy(string $strategy): void { $this->strategy = $strategy; diff --git a/src/Config/Apps/Http.php b/src/Config/Apps/Http.php index 8943d1a..0d0b733 100644 --- a/src/Config/Apps/Http.php +++ b/src/Config/Apps/Http.php @@ -63,7 +63,7 @@ public function toArray(): array } $servers = []; - array_map(static function (Server $server, string $key) use (&$servers) { + array_map(static function (Server $server, string $key) use (&$servers): void { $servers[$key] = $server->toArray(); }, $this->servers, array_keys($this->servers)); diff --git a/src/Config/Apps/Http/Server.php b/src/Config/Apps/Http/Server.php index 9ea1f2b..4100623 100644 --- a/src/Config/Apps/Http/Server.php +++ b/src/Config/Apps/Http/Server.php @@ -107,7 +107,7 @@ public function toArray(): array { $config = [ 'listen' => $this->listen, - 'routes' => [...array_map(static function (Route $route) { + 'routes' => [...array_map(static function (Route $route): array { return $route->toArray(); }, $this->routes) ], diff --git a/src/Config/Apps/Http/Server/Route.php b/src/Config/Apps/Http/Server/Route.php index 48c6250..2144c58 100644 --- a/src/Config/Apps/Http/Server/Route.php +++ b/src/Config/Apps/Http/Server/Route.php @@ -70,13 +70,13 @@ public function toArray(): array $config['group'] = $this->group; } - $config['handle'] = [...array_map(static function (HandlerInterface $handler) { + $config['handle'] = [...array_map(static function (HandlerInterface $handler): array { return $handler->toArray(); }, $this->handle) ]; if (isset($this->match)) { - $config['match'] = array_map(static function (MatcherInterface $matcher) { + $config['match'] = array_map(static function (MatcherInterface $matcher): array { return $matcher->toArray(); }, $this->match); diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Authentication.php b/src/Config/Apps/Http/Server/Routes/Handle/Authentication.php index 88b8c43..fe07adc 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Authentication.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Authentication.php @@ -26,8 +26,8 @@ public function toArray(): array 'handler' => $this->getHandler(), ]; - if (count($this->providers)) { - $config['providers'] = array_map(static function (ProviderInterface $provider) { + if ($this->providers !== []) { + $config['providers'] = array_map(static function (ProviderInterface $provider): array { return [$provider->getModuleName() => $provider->toArray()]; }, $this->providers)[0];//TODO there has to be a better way than [0] } diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic.php b/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic.php index 50399e2..2cb592e 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic.php @@ -31,8 +31,8 @@ public function toArray(): array { $config = []; - if (count($this->accounts)) { - $config['accounts'] = [...array_map(function (Account $account) { + if ($this->accounts !== []) { + $config['accounts'] = [...array_map(function (Account $account): array { return $account->toArray(); }, $this->accounts) ]; diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Headers/Request.php b/src/Config/Apps/Http/Server/Routes/Handle/Headers/Request.php index 11f2d75..36ba81d 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Headers/Request.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Headers/Request.php @@ -22,7 +22,6 @@ class Request implements Arrayable /** - * @param string $name * @param string[] $values * @return $this */ @@ -44,11 +43,11 @@ public function toArray(): array { $array = []; - if (count($this->delete)) { + if ($this->delete !== []) { $array['delete'] = $this->delete; } - if (count($this->add)) { + if ($this->add !== []) { $array['add'] = $this->add; } diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Headers/Response.php b/src/Config/Apps/Http/Server/Routes/Handle/Headers/Response.php index 28b2879..ce7a91b 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Headers/Response.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Headers/Response.php @@ -48,11 +48,11 @@ public function toArray(): array { $array = []; - if (count($this->delete)) { + if ($this->delete !== []) { $array['delete'] = $this->delete; } - if (count($this->add)) { + if ($this->add !== []) { $array['add'] = $this->add; } diff --git a/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy.php b/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy.php index 07a6f95..6968b2b 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy.php @@ -46,13 +46,13 @@ public function toArray(): array ]; if (isset($this->transport)) { - $array['transport'] = array_map(static function (TransportInterface $transport) { + $array['transport'] = array_map(static function (TransportInterface $transport): array { return $transport->toArray(); }, $this->transport)[0]; //TODO there has to be a better way than [0] access to get this level } if (isset($this->upstreams)) { - $array['upstreams'] = [...array_map(static function (Upstream $upstream) { + $array['upstreams'] = [...array_map(static function (Upstream $upstream): array { return $upstream->toArray(); }, $this->upstreams) ]; diff --git a/src/Config/Apps/Http/Server/Routes/Handle/StaticResponse.php b/src/Config/Apps/Http/Server/Routes/Handle/StaticResponse.php index b495fae..c146857 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/StaticResponse.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/StaticResponse.php @@ -20,7 +20,9 @@ class StaticResponse implements HandlerInterface public function __construct(?string $body = null, int $statusCode = 200) { - $body ? $this->body = $body : null; + if ($body) { + $this->body = $body; + } $this->statusCode = $statusCode; } diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Subroute.php b/src/Config/Apps/Http/Server/Routes/Handle/Subroute.php index 6e58572..7172aca 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Subroute.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Subroute.php @@ -24,7 +24,7 @@ public function toArray(): array { return [ 'handler' => $this->getHandler(), - 'routes' => [...array_map(static function (Route $route) { + 'routes' => [...array_map(static function (Route $route): array { return $route->toArray(); }, $this->routes) ], diff --git a/src/Config/Apps/Http/Server/Routes/Match/Not.php b/src/Config/Apps/Http/Server/Routes/Match/Not.php index 4516d6d..b2819c7 100644 --- a/src/Config/Apps/Http/Server/Routes/Match/Not.php +++ b/src/Config/Apps/Http/Server/Routes/Match/Not.php @@ -19,7 +19,7 @@ public function addNotMatcher(MatcherInterface $matcher): static public function toArray(): array { return [ - 'not' => array_map(static function (MatcherInterface $matcher) { + 'not' => array_map(static function (MatcherInterface $matcher): array { return $matcher->toArray(); }, $this->not), ]; diff --git a/src/Config/Apps/Tls/Automation.php b/src/Config/Apps/Tls/Automation.php index 1e1709b..7932170 100644 --- a/src/Config/Apps/Tls/Automation.php +++ b/src/Config/Apps/Tls/Automation.php @@ -40,7 +40,7 @@ public function toArray(): array } if (isset($this->policies)) { - $config['policies'] = array_map(function (Policies $policies) { + $config['policies'] = array_map(function (Policies $policies): array { return $policies->toArray(); }, $this->policies); } diff --git a/src/Config/Apps/Tls/Automation/Policies.php b/src/Config/Apps/Tls/Automation/Policies.php index fbdab3e..3c52e71 100644 --- a/src/Config/Apps/Tls/Automation/Policies.php +++ b/src/Config/Apps/Tls/Automation/Policies.php @@ -44,7 +44,7 @@ public function toArray(): array } if (isset($this->issuers)) { - $config['issuers'] = array_map(function (IssuerInterface $issuer) { + $config['issuers'] = array_map(function (IssuerInterface $issuer): array { return $issuer->toArray(); }, $this->issuers); } diff --git a/src/Config/Logging.php b/src/Config/Logging.php index e32b12e..5c714ac 100644 --- a/src/Config/Logging.php +++ b/src/Config/Logging.php @@ -15,7 +15,7 @@ class Logging implements Arrayable { /** @var Log[] $logs */ - private $logs = []; + private array $logs = []; public function addLog(Log $log, ?string $name = 'default'): static { @@ -31,7 +31,7 @@ public function toArray(): array { $logs = []; - array_map(function (string $key, Log $log) use (&$logs) { + array_map(function (string $key, Log $log) use (&$logs): void { $logs[$key] = $log->toArray(); }, array_keys($this->logs), $this->logs); diff --git a/src/Functions.php b/src/Functions.php index 8e014f1..b967099 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -8,8 +8,6 @@ /** * @param string|array|object|string>|object $objectToWalk - * @param string $hostToFind - * @param string $path * @return array{ * path: string, * host: Host @@ -17,25 +15,21 @@ */ function findHost(string|array|object $objectToWalk, string $hostToFind, string $path = ''): ?array { - if ($objectToWalk instanceof Host) { - if ( - $objectToWalk->getIdentifier() === $hostToFind && + if ( + $objectToWalk instanceof Host && ($objectToWalk->getIdentifier() === $hostToFind && str_contains($path, 'routes') && - str_contains($path, 'match') - ) { - return [ - 'path' => '/config/apps/http' . str_replace('_', '', $path) . '/host', - 'host' => &$objectToWalk, - ]; - } + str_contains($path, 'match')) + ) { + return [ + 'path' => '/config/apps/http' . str_replace('_', '', $path) . '/host', + 'host' => &$objectToWalk, + ]; } - if (is_object($objectToWalk)) { - if (method_exists($objectToWalk, 'iterateAllProperties')) { - $props = $objectToWalk->iterateAllProperties(); - if ($found = findHost($props, $hostToFind, $path)) { - return $found; - } + if (is_object($objectToWalk) && method_exists($objectToWalk, 'iterateAllProperties')) { + $props = $objectToWalk->iterateAllProperties(); + if ($found = findHost($props, $hostToFind, $path)) { + return $found; } }