Skip to content

Commit

Permalink
Move to extra strict rules for PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro committed Sep 30, 2022
1 parent 42621fe commit 4b215b7
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 36 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"nunomaduro/collision": "^5.3",
"nunomaduro/larastan": "^1.0",
"orchestra/testbench": "^6.19",
"phpstan/phpstan-strict-rules": "^1.4",
"phpunit/phpunit": "^9.3",
"spatie/laravel-ray": "^1.9"
},
Expand Down
15 changes: 15 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
includes:
- ./vendor/nunomaduro/larastan/extension.neon
- ./vendor/phpstan/phpstan-strict-rules/rules.neon

parameters:

Expand All @@ -16,3 +17,17 @@ parameters:
ignoreErrors:
- '#Internal.*#'
- '#Cannot access offset .* on array\{scheme.*#'

strictRules:
disallowedLooseComparison: true
booleansInConditions: true
uselessCast: true
requireParentConstructorCall: true
disallowedConstructs: true
overwriteVariablesWithLoop: true
closureUsesThis: true
matchingInheritedMethodNames: true
numericOperandsInArithmeticOperators: true
strictCalls: false
switchConditionsMatchingType: true
noVariableVariables: false
3 changes: 2 additions & 1 deletion src/Contracts/CDNService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Collection;
use A17\EdgeFlush\Services\Invalidation;
use Symfony\Component\HttpFoundation\Response;

interface CDNService extends Service
{
Expand All @@ -19,4 +18,6 @@ public function getInvalidationPathsForTags(
public function maxUrls(): int;

public function invalidationIsCompleted(string $invalidationId): bool;

public function enabled(): bool;
}
2 changes: 1 addition & 1 deletion src/Services/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function makeResponse(Response $response): Response

public function matchAny(string $string, array $patterns): bool
{
return !!collect($patterns)->reduce(
return (bool) collect($patterns)->reduce(
fn($matched, $pattern) => $matched ||
$this->match($pattern, $string),
false,
Expand Down
18 changes: 9 additions & 9 deletions src/Services/CacheControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function getContent(Response $response): string
if ($getContentFromResponse) {
if (method_exists($response, 'content')) {
$this->_content = $response->content();
} elseif (method_exists($response, 'getContent')) {
} else {
$this->_content = $response->getContent();
}

Expand Down Expand Up @@ -167,17 +167,17 @@ protected function containsValidForm(Response $response): bool
{
$hasForm = false;

if (config('edge-flush.valid_forms.enabled', false)) {
if (Helpers::configBool('edge-flush.valid_forms.enabled', false)) {
$hasForm = collect(
config('edge-flush.valid_forms.strings', false),
Helpers::configBool('edge-flush.valid_forms.strings', false),
)->reduce(function (bool $hasForm, string $string) use ($response) {
$string = Str::replace('%CSRF_TOKEN%', csrf_token(), $string);

return $hasForm && $this->contentContains($response, $string);
}, true);
}

return !!$hasForm;
return (bool) $hasForm;
}

protected function isFrontend(): bool
Expand Down Expand Up @@ -393,9 +393,9 @@ public function routeIsCachable(): bool

$route = EdgeFlush::getRequest()->route();

$route = $route instanceof Route ? $route->getName() : null;
$route = (string) ($route instanceof Route ? $route->getName() : null);

if (empty($route)) {
if (blank($route)) {
return Helpers::configBool(
'edge-flush.routes.cache_nameless_routes',
false,
Expand Down Expand Up @@ -458,7 +458,7 @@ public function stripCookies(Response $response, string $strategy): Response

public function getStrategyArray(string|null $strategyName): array
{
if (!$this->enabled() || empty($strategyName)) {
if (!$this->enabled() || $strategyName !== null) {
return Helpers::configArray('edge-flush.strategies.zero') ?? [];
}

Expand All @@ -467,7 +467,7 @@ public function getStrategyArray(string|null $strategyName): array
"edge-flush.built-in-strategies.$strategyName",
) ?? $strategyName;

if (empty($strategyName)) {
if ($strategyName !== null) {
return [];
}

Expand All @@ -488,7 +488,7 @@ public function willBeCached(
public function strategyDoesntContainsNoStoreDirectives(
string $strategy
): bool {
return !!collect(explode(',', $strategy))->reduce(function (
return (bool) collect(explode(',', $strategy))->reduce(function (
$willCache,
$element
) {
Expand Down
6 changes: 3 additions & 3 deletions src/Services/EdgeFlush.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ public function getRequest(): Request
public function storeTagsServiceIsEnabled(): bool
{
return $this->enabled() &&
config('edge-flush.enabled-services.store-tags', false);
Helpers::configBool('edge-flush.enabled-services.store-tags', false);
}

public function invalidationServiceIsEnabled(): bool
{
return $this->enabled() &&
config('edge-flush.enabled-services.invalidation', false);
Helpers::configBool('edge-flush.enabled-services.invalidation', false);
}

public function warmerServiceIsEnabled(): bool
{
return $this->enabled() && config('edge-flush.warmer.enabled', false);
return $this->enabled() && Helpers::configBool('edge-flush.warmer.enabled', false);
}
}
4 changes: 2 additions & 2 deletions src/Services/FrontendChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public function runningOnFrontend(): bool
{
$route = request()->route();

if (!$route instanceof Route || empty(($name = $route->getName()))) {
if (!$route instanceof Route || blank(($name = $route->getName()))) {
return false;
}

return Str::startsWith($name, ['front.', 'api.']);
return Str::startsWith((string) $name, ['front.', 'api.']);
}
}
4 changes: 2 additions & 2 deletions src/Services/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ protected function wasNotProcessed(Model $model): bool

$key = $model->getTable() . '-' . $id;

if ($this->processedTags[$key] ?? false) {
if (filled($this->processedTags[$key]) && (bool)$this->processedTags[$key]) {
return false;
}

Expand Down Expand Up @@ -466,7 +466,7 @@ public function dbStatement(string $sql): bool
public function enabled(): bool
{
return EdgeFlush::invalidationServiceIsEnabled() &&
EdgeFlush::cdn() !== null;
EdgeFlush::cdn()->enabled();
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Services/Warmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function dispatchExternalWarmRequests(Collection $urls): void

public function getGuzzle(): Guzzle
{
if (!empty($this->guzzle)) {
if ($this->guzzle instanceof Guzzle) {
return $this->guzzle;
}

Expand Down Expand Up @@ -185,7 +185,7 @@ public function getHeaders(string $url): array

public function isWarming(Request|null $request = null): bool
{
if (empty($request)) {
if (!$request instanceof Request) {
$request = EdgeFlush::getRequest();
}

Expand Down Expand Up @@ -213,21 +213,21 @@ public function getGuzzleConfiguration(): array

'curl' =>
[
CURLOPT_CONNECT_ONLY => config(
CURLOPT_CONNECT_ONLY => Helpers::configBool(
'edge-flush.warmer.curl.connect_only',
false,
),

CURLOPT_NOBODY => !config(
CURLOPT_NOBODY => !Helpers::configBool(
'edge-flush.warmer.curl.get_body',
true,
),

CURLOPT_ACCEPT_ENCODING => !config(
CURLOPT_ACCEPT_ENCODING => !Helpers::configBool(
'edge-flush.warmer.curl.compress',
true,
),
] + (array) config('edge-flush.warmer.curl.extra_options', []),
] + (array) config('edge-flush.warmer.extra_options');
] + (array) Helpers::configArray('edge-flush.warmer.curl.extra_options', []),
] + (array) Helpers::configArray('edge-flush.warmer.extra_options');
}
}
22 changes: 11 additions & 11 deletions src/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class Helpers
*/
public static function sanitizeUrl(string $url): string
{
if (config('edge-flush.urls.query.fully_cachable')) {
if (Helpers::configBool('edge-flush.urls.query.fully_cachable')) {
return $url;
}

$parsed = static::parseUrl($url);

$query = [];

parse_str($parsed['query'] ?? null, $query);
parse_str($parsed['query'] ?? '', $query);

if (blank($query)) {
return $url;
Expand All @@ -37,7 +37,7 @@ public static function sanitizeUrl(string $url): string
try {
$routeName = app('router')
->getRoutes()
->match(app('request')->create($url))
->match(request()->create($url))
->getName();
} catch (\Throwable) {
$routeName = '';
Expand Down Expand Up @@ -69,7 +69,7 @@ public static function rewriteUrl(

$query = [];

parse_str($url['query'] ?? '', $query);
parse_str($url['query'] ?? null, $query);

foreach ($parameters as $key => $parameter) {
$query[$key] = $parameter;
Expand All @@ -92,8 +92,8 @@ public static function rebuildUrl(array $components): string
$url = $components['scheme'] . '://';

if (
!empty($components['username']) &&
!empty($components['password'])
filled($components['username']) &&
filled($components['password'])
) {
$url .=
$components['username'] . ':' . $components['password'] . '@';
Expand All @@ -102,23 +102,23 @@ public static function rebuildUrl(array $components): string
$url .= $components['host'];

if (
!empty($components['port']) &&
filled($components['port']) &&
(($components['scheme'] === 'http' && $components['port'] !== 80) ||
($components['scheme'] === 'https' &&
$components['port'] !== 443))
) {
$url .= ':' . $components['port'];
}

if (!empty($components['path'])) {
if (filled($components['path'])) {
$url .= $components['path'];
}

if (!empty($components['fragment'])) {
if (filled($components['fragment'])) {
$url .= '#' . $components['fragment'];
}

if (!empty($components['query'])) {
if (filled($components['query'])) {
$url .= '?' . http_build_query($components['query']);
}

Expand Down Expand Up @@ -232,7 +232,7 @@ public static function toArray(mixed $array): array

public static function toBool(mixed $value): bool
{
return !!$value;
return (bool) $value;
}

public static function configBool(string $key, mixed $default = null): bool
Expand Down

0 comments on commit 4b215b7

Please sign in to comment.