Skip to content

Commit

Permalink
Replace cURL with HTTP (#33)
Browse files Browse the repository at this point in the history
* Replace curl with HTTP

* Replace curl with http in getRemoteFileSize method

* Change empty data check

* Fix styling

* Mark test skipped until we have a solution

* Fix styling

* Add try/catch to request

* Http class to handle remote response in one place

* Fix styling

* Safe usage of new static by returning self

---------

Co-authored-by: Baspa <[email protected]>
  • Loading branch information
Baspa and Baspa authored Aug 3, 2023
1 parent bf19f1e commit 29d7578
Show file tree
Hide file tree
Showing 29 changed files with 134 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/Checks/Configuration/NoFollowCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class NoFollowCheck implements Check

public bool $continueAfterFailure = false;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Configuration/NoIndexCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class NoIndexCheck implements Check

public bool $continueAfterFailure = false;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Configuration/RobotsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RobotsCheck implements Check

public bool $continueAfterFailure = false;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Content/AltTagCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AltTagCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Content/BrokenImageCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class BrokenImageCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Content/BrokenLinkCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class BrokenLinkCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
4 changes: 2 additions & 2 deletions src/Checks/Content/ContentLengthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ContentLengthCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand All @@ -47,7 +47,7 @@ public function check(Response $response, Crawler $crawler): bool
return $this->validateContent($content);
}

public function getContentToValidate(Response $response): string|null
public function getContentToValidate(Response $response): ?string
{
$url = $response->transferStats->getHandlerStats()['url'];

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Content/MixedContentCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MixedContentCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Content/MultipleHeadingCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MultipleHeadingCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Meta/DescriptionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DescriptionCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Meta/LangCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LangCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Meta/OpenGraphImageCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class OpenGraphImageCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Meta/TitleCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TitleCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Meta/TitleLengthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TitleLengthCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Performance/CompressionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CompressionCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Performance/CssSizeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CssSizeCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Performance/HtmlSizeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HtmlSizeCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Performance/ImageSizeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ImageSizeCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Performance/JavascriptSizeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class JavascriptSizeCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Performance/ResponseCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ResponseCheck implements Check

public bool $continueAfterFailure = false;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Checks/Performance/TtfbCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TtfbCheck implements Check

public bool $continueAfterFailure = true;

public string|null $failureReason;
public ?string $failureReason;

public mixed $actualValue = null;

Expand Down
4 changes: 2 additions & 2 deletions src/Commands/SeoScan.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private static function getRoutes(): Collection
return $routes;
}

private function calculateScoreForModel(string $model, ?string $scope = null): void
private function calculateScoreForModel(string $model, string $scope = null): void
{
$items = new $model;

Expand Down Expand Up @@ -198,7 +198,7 @@ private function calculateScoreForModel(string $model, ?string $scope = null): v
});
}

private function saveScoreToDatabase(SeoScore $seo, string $url, object|null $model = null): void
private function saveScoreToDatabase(SeoScore $seo, string $url, object $model = null): void
{
$score = $seo->getScore();

Expand Down
90 changes: 90 additions & 0 deletions src/Http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Vormkracht10\Seo;

use Illuminate\Support\Facades\Http as HttpFacade;

class Http
{
public string $url;

public array $options = [];

public array $headers = [];

public HttpFacade $http;

public function __construct(string $url)
{
$this->url = $url;
}

public static function make(string $url): self
{
return new self($url);
}

public function withOptions(array $options): self
{
$this->options = $options;

return $this;
}

public function withHeaders(array $headers): self
{
$this->headers = $headers;

return $this;
}

public function get(): object
{
return HttpFacade::withOptions([
...config('seo.http.options', []),
...$this->options,
])->withHeaders([
...config('seo.http.headers', []),
...$this->headers,
])->get($this->url);
}

public function getRemoteResponse(): object
{
$options = [
'timeout' => 30,
'return_transfer' => true,
'follow_location' => true,
'no_body' => true,
'header' => true,
];

if (app()->runningUnitTests()) {
$options = [
...$options,
'ssl_verifyhost' => false,
'ssl_verifypeer' => false,
'ssl_verifystatus' => false,
];
}

$domain = parse_url($this->url, PHP_URL_HOST);

if (in_array($domain, array_keys(config('seo.resolve')))) {
$port = str_contains($this->url, 'https://') ? 443 : 80;

$ipAddress = config('seo.resolve')[$domain];

if (! empty($ipAddress)) {
$options = [
...$options,
'resolve' => ["{$domain}:{$port}:{$ipAddress}"],
];
}
}

$this->withOptions($options);

return $this->get();
}
}
2 changes: 1 addition & 1 deletion src/Jobs/Scan.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Scan implements ShouldQueue

public $timeout = 60 * 60 * 3;

public function handle(string|null $url = null): void
public function handle(string $url = null): void
{
if (! $url) {
Artisan::call('seo:scan');
Expand Down
4 changes: 2 additions & 2 deletions src/Seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Seo
/**
* @var ProgressBar|null The progress bar to use for the checks.
*/
public ProgressBar|null $progress;
public ?ProgressBar $progress;

public string $url;

Expand All @@ -28,7 +28,7 @@ public function __construct(
) {
}

public function check(string $url, ProgressBar|null $progress = null): SeoScore
public function check(string $url, ProgressBar $progress = null): SeoScore
{
$this->progress = $progress;
$this->url = $url;
Expand Down
2 changes: 1 addition & 1 deletion src/SeoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface SeoInterface
{
public function getUrlAttribute(): string|null;
public function getUrlAttribute(): ?string;
}
2 changes: 1 addition & 1 deletion src/Traits/PerformCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait PerformCheck
{
public string|null $url = null;
public ?string $url = null;

public function __invoke(array $data, Closure $next)
{
Expand Down
Loading

0 comments on commit 29d7578

Please sign in to comment.