Skip to content

Commit

Permalink
implement rector
Browse files Browse the repository at this point in the history
  • Loading branch information
mattvb91 committed Feb 4, 2024
1 parent 91fe021 commit a443040
Show file tree
Hide file tree
Showing 22 changed files with 70 additions and 60 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 --dry-run",
"rector-fix": "rector"
},
"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.18.13"
}
}
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
23 changes: 23 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {

$rectorConfig->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,
]);
};
8 changes: 3 additions & 5 deletions src/Caddy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Apps/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 0 additions & 10 deletions src/Config/Apps/Cache/Cdn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Apps/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion src/Config/Apps/Http/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
],
Expand Down
4 changes: 2 additions & 2 deletions src/Config/Apps/Http/Server/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/Config/Apps/Http/Server/Routes/Handle/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
];
Expand Down
5 changes: 2 additions & 3 deletions src/Config/Apps/Http/Server/Routes/Handle/Headers/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Request implements Arrayable


/**
* @param string $name
* @param string[] $values
* @return $this
*/
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
];
Expand Down
4 changes: 3 additions & 1 deletion src/Config/Apps/Http/Server/Routes/Handle/StaticResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Config/Apps/Http/Server/Routes/Handle/Subroute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
],
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Apps/Http/Server/Routes/Match/Not.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Apps/Tls/Automation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Apps/Tls/Automation/Policies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Config/Logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);

Expand Down
30 changes: 12 additions & 18 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,28 @@

/**
* @param string|array<string, array<Http>|object|string>|object $objectToWalk
* @param string $hostToFind
* @param string $path
* @return array{
* path: string,
* host: Host
* }|null
*/
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;
}
}

Expand Down

0 comments on commit a443040

Please sign in to comment.