Skip to content

Commit

Permalink
implement rector (#17)
Browse files Browse the repository at this point in the history
* implement rector

* fix rector parallel process error and strict types declaration
  • Loading branch information
mattvb91 authored Feb 4, 2024
1 parent 91fe021 commit 40a4eb4
Show file tree
Hide file tree
Showing 63 changed files with 203 additions and 62 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 process --dry-run",
"rector-fix": "rector process"
},
"autoload": {
"psr-4": {
Expand All @@ -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.17"
}
}
4 changes: 2 additions & 2 deletions docker/composer/Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
26 changes: 26 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
]);

// register a single rule
$rectorConfig->rules([
InlineConstructorDefaultToPropertyRector::class,
DeclareStrictTypesRector::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,
]);
};
24 changes: 17 additions & 7 deletions src/Caddy.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp;

use GuzzleHttp\Client;
Expand Down Expand Up @@ -72,7 +74,12 @@ public function syncHosts(string $hostIdentifier): void
$this->buildHostsCache($hostIdentifier);

/** @var string[] $hosts */
$hosts = json_decode($this->client->get($this->hostsCache[$hostIdentifier]['path'])->getBody(), true);
$hosts = json_decode(
$this->client->get($this->hostsCache[$hostIdentifier]['path'])
->getBody()
->getContents(),
true
);

$this->hostsCache[$hostIdentifier]['host']->setHosts($hosts);
}
Expand Down Expand Up @@ -129,7 +136,12 @@ public function removeHostname(string $hostIdentifier, string $hostname): bool
public function getRemoteConfig(): object
{
/** @var object */
return json_decode($this->client->get('/config')->getBody(), false, 512, JSON_THROW_ON_ERROR);
return json_decode(
$this->client->get('/config')->getBody()->getContents(),
false,
512,
JSON_THROW_ON_ERROR
);
}

/**
Expand Down Expand Up @@ -217,10 +229,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));

Expand All @@ -231,13 +243,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;
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Admin.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config;

use mattvb91\CaddyPhp\Interfaces\Arrayable;
Expand Down
4 changes: 3 additions & 1 deletion src/Config/Apps/Cache.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps;

use mattvb91\CaddyPhp\Config\Apps\Cache\Api;
Expand Down Expand Up @@ -116,7 +118,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
}
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Apps/Cache/Api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Cache;

use mattvb91\CaddyPhp\Config\Apps\Cache\Api\Souin;
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Apps/Cache/Api/Souin.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Cache\Api;

use mattvb91\CaddyPhp\Interfaces\Arrayable;
Expand Down
12 changes: 2 additions & 10 deletions src/Config/Apps/Cache/Cdn.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Cache;

use mattvb91\CaddyPhp\Interfaces\Arrayable;
Expand All @@ -9,27 +11,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;
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Apps/Cache/Key.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Cache;

use mattvb91\CaddyPhp\Interfaces\Arrayable;
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Apps/Cache/Nuts.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Cache;

use mattvb91\CaddyPhp\Interfaces\Arrayable;
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Apps/Cache/Redis.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Cache;

use mattvb91\CaddyPhp\Interfaces\Arrayable;
Expand Down
4 changes: 3 additions & 1 deletion src/Config/Apps/Http.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps;

use mattvb91\CaddyPhp\Config\Apps\Http\Server;
Expand Down Expand Up @@ -63,7 +65,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));

Expand Down
4 changes: 3 additions & 1 deletion src/Config/Apps/Http/Server.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Http;

use mattvb91\CaddyPhp\Config\Apps\Http\Server\Route;
Expand Down Expand Up @@ -107,7 +109,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)
],
Expand Down
6 changes: 4 additions & 2 deletions src/Config/Apps/Http/Server/Route.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Http\Server;

use mattvb91\CaddyPhp\Interfaces\Apps\Servers\Routes\Handle\HandlerInterface;
Expand Down Expand Up @@ -70,13 +72,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);

Expand Down
6 changes: 4 additions & 2 deletions src/Config/Apps/Http/Server/Routes/Handle/Authentication.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle;

use mattvb91\CaddyPhp\Interfaces\Apps\Servers\Routes\Handle\Authentication\ProviderInterface;
Expand All @@ -26,8 +28,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]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\Authentication\Providers;

use mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\Authentication\Providers\HttpBasic\Account;
Expand Down Expand Up @@ -31,8 +33,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)
];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\Authentication\Providers\HttpBasic;

use mattvb91\CaddyPhp\Interfaces\Arrayable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\Authentication\Providers\HttpBasic\Hash;

use mattvb91\CaddyPhp\Interfaces\Apps\Servers\Routes\Handle\Authentication\Providers\HttpBasic\HashInterface;
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Apps/Http/Server/Routes/Handle/Cache.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle;

use mattvb91\CaddyPhp\Config\Logs\LogLevel;
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Apps/Http/Server/Routes/Handle/Error.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle;

use mattvb91\CaddyPhp\Interfaces\Apps\Servers\Routes\Handle\HandlerInterface;
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Apps/Http/Server/Routes/Handle/FileServer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle;

use mattvb91\CaddyPhp\Interfaces\Apps\Servers\Routes\Handle\HandlerInterface;
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Apps/Http/Server/Routes/Handle/Headers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle;

use mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\Headers\Request;
Expand Down
Loading

0 comments on commit 40a4eb4

Please sign in to comment.