Skip to content

Commit

Permalink
feat: setup and applied rector
Browse files Browse the repository at this point in the history
  • Loading branch information
shavounet committed Oct 13, 2023
1 parent 866adaf commit 0875c78
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 91 deletions.
25 changes: 23 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Continuous Integration
on: [push, pull_request]
on: [ push, pull_request ]

jobs:
linter:
Expand Down Expand Up @@ -65,4 +65,25 @@ jobs:
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- run: composer update --prefer-dist --no-interaction ${{ matrix.flags }}
- run: bin/phpstan
- run: bin/phpstan

rector:
name: Rector checks
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@master
- uses: shivammathur/setup-php@v2
with:
php-version: 8.1
coverage: xdebug2
- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- run: composer update --prefer-dist --no-interaction
- run: bin/rector --dry-run
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"friendsofphp/php-cs-fixer": "3.35.1",
"guzzlehttp/promises": "^2",
"phpstan/phpstan": "1.10.38",
"phpstan/phpstan-symfony": "1.3.4"
"phpstan/phpstan-symfony": "1.3.4",
"rector/rector": "0.18.5"
},
"autoload": {
"psr-4": {"M6Web\\Bundle\\GuzzleHttpBundle\\": "src/"}
Expand Down
29 changes: 29 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Symfony\Set\SymfonySetList;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Symfony\Set\SensiolabsSetList;

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

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
SymfonySetList::SYMFONY_54,
SymfonySetList::SYMFONY_CODE_QUALITY,
DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
SensiolabsSetList::ANNOTATIONS_TO_ATTRIBUTES,
]);
};
2 changes: 1 addition & 1 deletion src/DataCollector/GuzzleHttpDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class GuzzleHttpDataCollector extends DataCollector
{
/** @var GuzzleHttpClient[] */
private $clients = [];
private array $clients = [];

public function __construct()
{
Expand Down
36 changes: 9 additions & 27 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,26 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('body')->end()
->variableNode('cert')
->validate()
->ifTrue(function ($v) {
return !\is_string($v) && (!\is_array($v) || \count($v) != 2);
})
->ifTrue(fn($v) => !\is_string($v) && (!\is_array($v) || \count($v) != 2))
->theninvalid('Requires a string or a two entries array')
->end()
->end()
->variableNode('debug')
->validate()
->ifTrue(function ($v) {
return !\is_string($v) && !\is_bool($v);
})
->ifTrue(fn($v) => !\is_string($v) && !\is_bool($v))
->theninvalid('Requires an invokable service id or a bolean value')
->end()
->end()
->variableNode('decode_content')
->validate()
->ifTrue(function ($v) {
return !\is_string($v) && !\is_bool($v);
})
->ifTrue(fn($v) => !\is_string($v) && !\is_bool($v))
->theninvalid('Requires a string or a boolean')
->end()
->end()
->floatNode('delay')->end()
->variableNode('expect')
->validate()
->ifTrue(function ($v) {
return !\is_int($v) && !\is_bool($v);
})
->ifTrue(fn($v) => !\is_int($v) && !\is_bool($v))
->theninvalid('Requires an integer or a boolean')
->end()
->end()
Expand All @@ -91,9 +83,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->variableNode('cookies')
->validate()
->ifTrue(function ($v) {
return !\is_array($v) && !\is_bool($v);
})
->ifTrue(fn($v) => !\is_array($v) && !\is_bool($v))
->theninvalid('Requires an array or a boolean')
->end()
->end()
Expand All @@ -114,9 +104,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('on_stats')->end()
->variableNode('proxy')
->validate()
->ifTrue(function ($v) {
return !\is_array($v) && !\is_string($v);
})
->ifTrue(fn($v) => !\is_array($v) && !\is_string($v))
->theninvalid('Requires an array or a string')
->end()
->end()
Expand All @@ -127,29 +115,23 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('sink')->end()
->variableNode('ssl_key')
->validate()
->ifTrue(function ($v) {
return !\is_string($v) && (!\is_array($v) || \count($v) != 2);
})
->ifTrue(fn($v) => !\is_string($v) && (!\is_array($v) || \count($v) != 2))
->theninvalid('Requires a string or a two entries array')
->end()
->end()
->booleanNode('stream')->end()
->booleanNode('synchronous')->end()
->variableNode('verify')
->validate()
->ifTrue(function ($v) {
return !\is_string($v) && !\is_bool($v);
})
->ifTrue(fn($v) => !\is_string($v) && !\is_bool($v))
->theninvalid('Requires a string or a boolean')
->end()
->end()
->scalarNode('version')->end()
->arrayNode('allow_redirects')
->beforeNormalization()
->ifInArray([true, false])
->then(function ($v) {
return ['max' => 0];
})
->then(fn($v) => ['max' => 0])
->end()
->addDefaultsIfNotSet()
->children()
Expand Down
6 changes: 1 addition & 5 deletions src/DependencyInjection/GuzzleHttpPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@

class GuzzleHttpPass implements CompilerPassInterface
{
/** @var string */
private $clientTag;

public function __construct(string $clientTag = 'm6web_guzzlehttp.client')
public function __construct(private readonly string $clientTag = 'm6web_guzzlehttp.client')
{
$this->clientTag = $clientTag;
}

public function process(ContainerBuilder $container)
Expand Down
8 changes: 3 additions & 5 deletions src/DependencyInjection/M6WebGuzzleHttpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ protected function getCurlConfig(array $config)

protected function getServiceReference(string $id)
{
if (substr($id, 0, 1) == '@') {
if (str_starts_with($id, '@')) {
return new Reference(substr($id, 1));
}

Expand All @@ -238,9 +238,7 @@ protected function getCookiesJarServiceReference(ContainerBuilder $container, ar
array_walk($cookies, function (&$item) {
$item = array_combine(
array_map(
function ($key) {
return ucwords($key, ' -');
},
fn($key) => ucwords($key, ' -'),
array_keys($item)
),
array_values($item)
Expand All @@ -249,7 +247,7 @@ function ($key) {

$container->register(
$id = sprintf('m6web_guzzlehttp.guzzle.cookies_jar.%s', $clientId),
'GuzzleHttp\Cookie\CookieJar'
\GuzzleHttp\Cookie\CookieJar::class
)
->setArguments([false, $cookies])
->setPublic(true);
Expand Down
4 changes: 2 additions & 2 deletions src/EventDispatcher/AbstractGuzzleCacheEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
abstract class AbstractGuzzleCacheEvent extends Event
{
/** @const string Name of standard event */
public const NAME = 'm6web.guzzlecache';
final public const NAME = 'm6web.guzzlecache';

/** @const string Name of Error event */
public const NAME_ERROR = 'm6web.guzzlecache.error';
final public const NAME_ERROR = 'm6web.guzzlecache.error';

/** @var RequestInterface */
protected $request;
Expand Down
4 changes: 2 additions & 2 deletions src/EventDispatcher/AbstractGuzzleHttpEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
abstract class AbstractGuzzleHttpEvent extends Event
{
public const EVENT_NAME = 'm6web.guzzlehttp';
public const EVENT_ERROR_NAME = 'm6web.guzzlehttp.error';
final public const EVENT_NAME = 'm6web.guzzlehttp';
final public const EVENT_ERROR_NAME = 'm6web.guzzlehttp.error';

/** @var float Command start time */
protected $executionStart;
Expand Down
9 changes: 3 additions & 6 deletions src/Handler/CacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,11 @@ protected static function getKey(RequestInterface $request)
// Generate headerline for the cache key. X- headers are ignored except those included in the Vary
$vary = array_flip($request->getHeader('Vary'));
$headerLine = implode('', array_map(
[$request, 'getHeaderLine'],
$request->getHeaderLine(...),
array_filter(
array_keys($request->getHeaders()),
function ($header) use ($vary) {
return 0 !== stripos($header, 'x-')
|| \array_key_exists($header, $vary)
;
}
fn($header) => 0 !== stripos((string) $header, 'x-')
|| \array_key_exists($header, $vary)
)
));

Expand Down
41 changes: 17 additions & 24 deletions src/Middleware/EventDispatcherMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,16 @@ class EventDispatcherMiddleware implements MiddlewareInterface
protected $eventDispatcher;

/** @var array */
protected $events;

/** @var string */
protected $clientId;
protected $events = [];

/**
* Constructor
*
* @param string $clientId
*/
public function __construct(EventDispatcherInterface $eventDispatcher, $clientId)
public function __construct(EventDispatcherInterface $eventDispatcher, protected $clientId)
{
$this->eventDispatcher = $eventDispatcher;
$this->events = [];
$this->clientId = $clientId;
}

/**
Expand All @@ -44,26 +39,24 @@ public function __construct(EventDispatcherInterface $eventDispatcher, $clientId
*/
public function push(HandlerStack $stack)
{
$stack->push(function (callable $handler) {
return function (
RequestInterface $request,
array $options
) use ($handler) {
$promise = $handler($request, $options);
$stack->push(fn(callable $handler) => function (
RequestInterface $request,
array $options
) use ($handler) {
$promise = $handler($request, $options);

return $promise->then(
function (ResponseInterface $response) use ($request) {
$this->sendEvent($request, $response);
return $promise->then(
function (ResponseInterface $response) use ($request) {
$this->sendEvent($request, $response);

return $response;
},
function (\Exception $reason) use ($request) {
$this->sendErrorEvent($request, $reason);
return $response;
},
function (\Exception $reason) use ($request): never {
$this->sendErrorEvent($request, $reason);

throw $reason;
}
);
};
throw $reason;
}
);
}, 'eventDispatcher_dispatch');

return $stack;
Expand Down
27 changes: 11 additions & 16 deletions src/Middleware/HostForwarderMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,27 @@
*/
class HostForwarderMiddleware implements MiddlewareInterface
{
protected array $config;

public function __construct(array $config)
public function __construct(protected array $config)
{
$this->config = $config;
}

/**
* Push function to middleware handler
*/
public function push(HandlerStack $stack): HandlerStack
{
$stack->push(function (callable $handler) {
return function (
RequestInterface $request,
array $options
) use ($handler) {
if (isset($this->config['headers'])) {
$lowercaseHeaders = array_change_key_case($this->config['headers']);
if (isset($lowercaseHeaders['host'])) {
$request = $request->withHeader('host', $lowercaseHeaders['host']);
}
$stack->push(fn(callable $handler) => function (
RequestInterface $request,
array $options
) use ($handler) {
if (isset($this->config['headers'])) {
$lowercaseHeaders = array_change_key_case($this->config['headers']);
if (isset($lowercaseHeaders['host'])) {
$request = $request->withHeader('host', $lowercaseHeaders['host']);
}
}

return $handler($request, $options);
};
return $handler($request, $options);
}, 'hostForwarder_forward');

return $stack;
Expand Down

0 comments on commit 0875c78

Please sign in to comment.