Skip to content

Commit f1a892b

Browse files
committed
added Laravel Pint
1 parent 44b6e74 commit f1a892b

16 files changed

+115
-155
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"spatie/phpunit-snapshot-assertions": "^4.2.16",
4242
"symfony/var-dumper": "^5.4.21|^6.2.7",
4343
"mockery/mockery": "^1.5.1",
44-
"phpstan/phpstan": "^1.10.6"
44+
"phpstan/phpstan": "^1.10.6",
45+
"laravel/pint": "^1.13"
4546
},
4647
"autoload": {
4748
"psr-4": {
@@ -54,8 +55,7 @@
5455
]
5556
},
5657
"scripts": {
57-
"analyze": [
58-
".\\vendor\\bin\\phpstan analyse"
59-
]
58+
"analyze": "./vendor/bin/phpstan analyse",
59+
"pint": "./vendor/bin/pint"
6060
}
6161
}

src/FeedDiscoverers/FeedDiscoverer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
interface FeedDiscoverer
1010
{
1111
/**
12-
* @param Client $httpClient
13-
* @param ResponseContainer $responseContainer
1412
* @return Collection<int, string>
1513
*/
1614
public function discover(Client $httpClient, ResponseContainer $responseContainer): Collection;

src/FeedDiscoverers/FeedDiscovererByContentType.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
class FeedDiscovererByContentType implements FeedDiscoverer
1515
{
1616
/**
17-
* @param Client $httpClient
18-
* @param ResponseContainer $responseContainer
1917
* @return Collection<int, string>
2018
*/
2119
public function discover(Client $httpClient, ResponseContainer $responseContainer): Collection
@@ -25,7 +23,7 @@ public function discover(Client $httpClient, ResponseContainer $responseContaine
2523
$contentType = is_array($contentTypeMixedValue) ? Arr::first($contentTypeMixedValue) : $contentTypeMixedValue;
2624

2725
// the given url is no valid RSS feed
28-
if (!$contentType || !Str::startsWith($contentType, ['application/rss+xml', 'application/atom+xml'])) {
26+
if (! $contentType || ! Str::startsWith($contentType, ['application/rss+xml', 'application/atom+xml'])) {
2927
return new Collection();
3028
}
3129

src/FeedDiscoverers/FeedDiscovererByFeedly.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@ class FeedDiscovererByFeedly implements FeedDiscoverer
1616
private const FEEDLY_API_BASE_URL = 'https://cloud.feedly.com/v3';
1717

1818
/**
19-
* @param Client $httpClient
20-
* @param ResponseContainer $responseContainer
2119
* @return Collection<int, string>
20+
*
2221
* @throws \GuzzleHttp\Exception\GuzzleException
2322
*/
2423
public function discover(Client $httpClient, ResponseContainer $responseContainer): Collection
2524
{
26-
$response = $httpClient->get(self::FEEDLY_API_BASE_URL . '/search/feeds', [
25+
$response = $httpClient->get(self::FEEDLY_API_BASE_URL.'/search/feeds', [
2726
'query' => ['query' => $responseContainer->getRequestUrl()],
2827
]);
2928

3029
$searchResponse = SearchResponse::fromJson(json_decode($response->getBody()->getContents(), true));
3130

32-
return $searchResponse->getResults()->map(fn(Result $result) => $result->getFeedUrl());
31+
return $searchResponse->getResults()->map(fn (Result $result) => $result->getFeedUrl());
3332
}
3433
}

src/FeedDiscoverers/FeedDiscovererByHtmlAnchorElements.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
class FeedDiscovererByHtmlAnchorElements implements FeedDiscoverer
1717
{
1818
/**
19-
* @param Client $httpClient
20-
* @param ResponseContainer $responseContainer
2119
* @return Collection<int, string>
2220
*/
2321
public function discover(Client $httpClient, ResponseContainer $responseContainer): Collection
@@ -26,7 +24,7 @@ public function discover(Client $httpClient, ResponseContainer $responseContaine
2624
$crawler = new Crawler($responseContainer->getResponse()->getBody()->getContents());
2725
$nodes = $crawler->filterXPath($cssConverter->toXPath('a'));
2826

29-
return (new Collection($nodes->each(fn(Crawler $node) => Helper::transformNodeToUrl($responseContainer->getRequestUrl(), $node))))
30-
->filter(fn($url) => Str::contains($url, 'rss'));
27+
return (new Collection($nodes->each(fn (Crawler $node) => Helper::transformNodeToUrl($responseContainer->getRequestUrl(), $node))))
28+
->filter(fn ($url) => Str::contains($url, 'rss'));
3129
}
3230
}

src/FeedDiscoverers/FeedDiscovererByHtmlHeadElements.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
class FeedDiscovererByHtmlHeadElements implements FeedDiscoverer
1616
{
1717
/**
18-
* @param Client $httpClient
19-
* @param ResponseContainer $responseContainer
2018
* @return Collection<int, string>
2119
*/
2220
public function discover(Client $httpClient, ResponseContainer $responseContainer): Collection
@@ -26,6 +24,6 @@ public function discover(Client $httpClient, ResponseContainer $responseContaine
2624

2725
$nodes = $crawler->filterXPath($cssConverter->toXPath('head > link[type="application/rss+xml"], head > link[type="application/atom+xml"]'));
2826

29-
return new Collection($nodes->each(fn(Crawler $node) => Helper::transformNodeToUrl($responseContainer->getRequestUrl(), $node)));
27+
return new Collection($nodes->each(fn (Crawler $node) => Helper::transformNodeToUrl($responseContainer->getRequestUrl(), $node)));
3028
}
3129
}

src/Hash.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,13 @@ enum Hash: string
5252

5353
case HAVAL_256_5 = 'haval256,5';
5454

55-
/**
56-
* @param self $algo
57-
* @param mixed $value
58-
* @return string|null
59-
*/
6055
public static function hash(self $algo, mixed $value): ?string
6156
{
6257
$availableAlgos = array_map(fn (self $hash) => $hash->value, self::cases());
6358
$availableAlgosOnMachine = hash_algos();
6459

65-
if (!in_array($algo->value, $availableAlgos, true) || !in_array($algo->value, $availableAlgosOnMachine, true)) {
66-
throw new InvalidArgumentException('The chosen hash algorithm is not supported: ' . $algo->value);
60+
if (! in_array($algo->value, $availableAlgos, true) || ! in_array($algo->value, $availableAlgosOnMachine, true)) {
61+
throw new InvalidArgumentException('The chosen hash algorithm is not supported: '.$algo->value);
6762
}
6863

6964
return hash($algo->value, $value);

src/Helper.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class Helper
1515
{
1616
public static function isValidUrl(string $url): bool
1717
{
18-
return (bool)filter_var($url, FILTER_VALIDATE_URL);
18+
return (bool) filter_var($url, FILTER_VALIDATE_URL);
1919
}
2020

2121
public static function normalizeUrl(string $url): string
2222
{
23-
return trim(preg_replace('#(^|[^:])//+#', "\\1/", $url), '/');
23+
return trim(preg_replace('#(^|[^:])//+#', '\\1/', $url), '/');
2424
}
2525

2626
public static function trimOrDefaultNull(?string $str): ?string
@@ -38,13 +38,13 @@ public static function transformUrl(string $baseUrl, string $url): string
3838
}
3939

4040
if (Str::startsWith($url, '/')) {
41-
return $baseUrl . '/' . $url;
41+
return $baseUrl.'/'.$url;
4242
}
4343

4444
$scheme = parse_url($baseUrl, PHP_URL_SCHEME);
4545
$host = parse_url($baseUrl, PHP_URL_HOST);
4646

47-
return $scheme . '://' . $host . '/' . $url;
47+
return $scheme.'://'.$host.'/'.$url;
4848
}
4949

5050
public static function transformNodeToUrl(string $baseUrl, Crawler $node): string
@@ -54,26 +54,20 @@ public static function transformNodeToUrl(string $baseUrl, Crawler $node): strin
5454

5555
public static function replaceBaseUrl(string $url, string $oldBaseUrl, string $newBaseUrl): string
5656
{
57-
return preg_replace('/^' . preg_quote($oldBaseUrl, '/') . '/', $newBaseUrl, $url);
57+
return preg_replace('/^'.preg_quote($oldBaseUrl, '/').'/', $newBaseUrl, $url);
5858
}
5959

6060
/**
61-
* @param string $url
62-
* @param string[] $urlReplacementMap
63-
* @return string
61+
* @param string[] $urlReplacementMap
6462
*/
6563
public static function replaceBaseUrls(string $url, array $urlReplacementMap): string
6664
{
6765
return (new Collection($urlReplacementMap))->keys()->reduce(
68-
fn($carry, $oldBaseUrl) => self::replaceBaseUrl($carry, $oldBaseUrl, $urlReplacementMap[$oldBaseUrl]), $url
66+
fn ($carry, $oldBaseUrl) => self::replaceBaseUrl($carry, $oldBaseUrl, $urlReplacementMap[$oldBaseUrl]), $url
6967
);
7068
}
7169

7270
/**
73-
* @param callable $callback
74-
* @param int $delay
75-
* @param int $retries
76-
* @return mixed
7771
* @throws Exception
7872
*/
7973
public static function withRetries(callable $callback, int $delay = 1, int $retries = 3): mixed
@@ -92,28 +86,24 @@ public static function withRetries(callable $callback, int $delay = 1, int $retr
9286
}
9387

9488
/**
95-
* @param mixed $value
96-
* @return Carbon|null
89+
* @param mixed $value
9790
*/
9891
public static function parseDate($value): ?Carbon
9992
{
10093
return $value === null ? null : Carbon::parse($value);
10194
}
10295

10396
/**
104-
* @param string $feedItemUrl
105-
* @param string|null $content
106-
* @param Client $httpClient
10797
* @return Collection<int, string>
10898
*/
10999
public static function getImageUrlsForFeedItem(string $feedItemUrl, ?string $content, Client $httpClient): Collection
110100
{
111101
$urlScheme = parse_url($feedItemUrl, PHP_URL_SCHEME);
112102
$urlHost = parse_url($feedItemUrl, PHP_URL_HOST);
113103

114-
$baseUrl = $urlScheme . '://' . $urlHost;
104+
$baseUrl = $urlScheme.'://'.$urlHost;
115105

116-
if (!$content) {
106+
if (! $content) {
117107
return new Collection();
118108
}
119109

@@ -128,9 +118,9 @@ public static function getImageUrlsForFeedItem(string $feedItemUrl, ?string $con
128118
return $imageUrl;
129119
}
130120

131-
return $baseUrl . '/' . ltrim($imageUrl, '/');
121+
return $baseUrl.'/'.ltrim($imageUrl, '/');
132122
})
133-
->filter(fn(string $imageUrl) => self::getHttpContentTypeForUrl($imageUrl, $httpClient) !== 'image/gif');
123+
->filter(fn (string $imageUrl) => self::getHttpContentTypeForUrl($imageUrl, $httpClient) !== 'image/gif');
134124
}
135125

136126
public static function getHttpContentTypeForUrl(string $url, Client $httpClient): ?string
@@ -152,18 +142,18 @@ public static function getHttpContentTypeForUrl(string $url, Client $httpClient)
152142
/**
153143
* Trims all collection values and filters out NULL values.
154144
*
155-
* @param Collection<int, string|null> $collection
145+
* @param Collection<int, string|null> $collection
156146
* @return Collection<int, string>
157147
*/
158148
public static function trimStringCollection(Collection $collection): Collection
159149
{
160150
/** @phpstan-ignore-next-line */
161-
return $collection->map(fn($category) => Helper::trimOrDefaultNull($category))->filter();
151+
return $collection->map(fn ($category) => Helper::trimOrDefaultNull($category))->filter();
162152
}
163153

164154
public static function entityDecode(string $encoding, ?string $str): ?string
165155
{
166-
if (!$str) {
156+
if (! $str) {
167157
return $str;
168158
}
169159

src/HeraRssCrawler.php

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class HeraRssCrawler
5959
public function __construct()
6060
{
6161
$name = Str::snake(Arr::last(explode('\\', self::class)));
62-
$userAgent = $name . '/' . self::VERSION;
62+
$userAgent = $name.'/'.self::VERSION;
6363

6464
$this->httpClient = new Client([
6565
'headers' => [
@@ -97,7 +97,7 @@ public function setRetryCount(int $retryCount): void
9797
}
9898

9999
/**
100-
* @param string[] $urlReplacementMap
100+
* @param string[] $urlReplacementMap
101101
*/
102102
public function setUrlReplacementMap(array $urlReplacementMap): void
103103
{
@@ -115,15 +115,14 @@ public function setLogger(LoggerInterface $logger): void
115115
/**
116116
* Set feed discoverer classes.
117117
*
118-
* @param Collection<int, mixed> $feedDiscoverers
119-
* @return void
118+
* @param Collection<int, mixed> $feedDiscoverers
120119
*/
121120
public function setFeedDiscoverers(Collection $feedDiscoverers): void
122121
{
123122
// make sure that every discoverer implements the FeedDiscoverer interface
124123
$feedDiscoverers->each(function ($discoverer) {
125-
if (!is_subclass_of($discoverer, FeedDiscoverer::class)) {
126-
throw new InvalidArgumentException($discoverer::class . ' is not a valid feed discoverer.');
124+
if (! is_subclass_of($discoverer, FeedDiscoverer::class)) {
125+
throw new InvalidArgumentException($discoverer::class.' is not a valid feed discoverer.');
127126
}
128127
});
129128

@@ -149,23 +148,23 @@ public function parseFeed(string $url): Feed
149148
/**
150149
* Discover the first feed URL of a website and parse the feed.
151150
*
152-
* @param string $url
153151
* @return Collection<int, Feed>
152+
*
154153
* @throws Exception
155154
*/
156155
public function discoverAndParseFeeds(string $url): Collection
157156
{
158157
return $this->withRetries(
159-
fn() => $this->discoverFeedUrls($url)
160-
->map(fn($feedUrl) => $this->parseFeed($feedUrl))
158+
fn () => $this->discoverFeedUrls($url)
159+
->map(fn ($feedUrl) => $this->parseFeed($feedUrl))
161160
);
162161
}
163162

164163
/**
165164
* Discover all available feed URLs of a website.
166165
*
167-
* @param string $url
168166
* @return Collection<int, string>
167+
*
169168
* @throws Exception
170169
*/
171170
public function discoverFeedUrls(string $url): Collection
@@ -186,7 +185,7 @@ public function discoverFeedUrls(string $url): Collection
186185
return $carry;
187186
}, new Collection());
188187

189-
return $urls->map(fn($adjustedUrl) => Helper::normalizeUrl($adjustedUrl))->unique()->values();
188+
return $urls->map(fn ($adjustedUrl) => Helper::normalizeUrl($adjustedUrl))->unique()->values();
190189
});
191190
}
192191

@@ -204,9 +203,9 @@ public function discoverFavicon(string $url): ?string
204203
$nodes = $crawler->filterXPath($this->cssConverter->toXPath('head > link'));
205204

206205
$faviconUrls = (new Collection($nodes))
207-
->filter(fn(DOMElement $node) => Str::contains($node->getAttribute('rel'), 'icon')) /** @phpstan-ignore-line */
208-
->map(fn(DOMElement $node) => Helper::normalizeUrl(Helper::transformUrl($url, $node->getAttribute('href')))) /** @phpstan-ignore-line */
209-
->filter(fn(string $imageUrl) => Helper::getHttpContentTypeForUrl($imageUrl, $this->httpClient) !== null);
206+
->filter(fn (DOMElement $node) => Str::contains($node->getAttribute('rel'), 'icon')) /** @phpstan-ignore-line */
207+
->map(fn (DOMElement $node) => Helper::normalizeUrl(Helper::transformUrl($url, $node->getAttribute('href')))) /** @phpstan-ignore-line */
208+
->filter(fn (string $imageUrl) => Helper::getHttpContentTypeForUrl($imageUrl, $this->httpClient) !== null);
210209

211210
if ($faviconUrls->isEmpty()) {
212211
return null;
@@ -222,10 +221,10 @@ public function discoverFavicon(string $url): ?string
222221
public function checkIfConsumableFeed(string $url): bool
223222
{
224223
try {
225-
return $this->withRetries(fn() => $this->parseFeed($url) instanceof Feed);
224+
return $this->withRetries(fn () => $this->parseFeed($url) instanceof Feed);
226225
} catch (Exception $e) {
227226
if ($this->logger) {
228-
$this->logger->error('Feed not consumable: ' . $e->getMessage());
227+
$this->logger->error('Feed not consumable: '.$e->getMessage());
229228
}
230229

231230
return false;
@@ -253,8 +252,8 @@ public static function generateChecksumForFeedItem(FeedItem $feedItem, string $d
253252

254253
$class = new ReflectionClass(FeedItem::class);
255254
$allValuesConcatenated = trim((new Collection($class->getMethods(ReflectionMethod::IS_PUBLIC)))
256-
->filter(fn(ReflectionMethod $method) => in_array($method->getName(), array_map(fn($property) => 'get' . Str::ucfirst($property), $properties), true))
257-
->reduce(fn($carry, ReflectionMethod $method) => $carry . $delimiter . $method->invoke($feedItem), ''), $delimiter);
255+
->filter(fn (ReflectionMethod $method) => in_array($method->getName(), array_map(fn ($property) => 'get'.Str::ucfirst($property), $properties), true))
256+
->reduce(fn ($carry, ReflectionMethod $method) => $carry.$delimiter.$method->invoke($feedItem), ''), $delimiter);
258257

259258
return Hash::hash($algo, $allValuesConcatenated);
260259
}
@@ -284,25 +283,23 @@ public static function generateChecksumForFeed(Feed $feed, string $delimiter = '
284283
$allValuesConcatenated = trim((new Collection($class->getMethods(ReflectionMethod::IS_PUBLIC)))
285284
->filter(function (ReflectionMethod $method) use ($properties) {
286285
return in_array($method->getName(), array_map(function ($property) {
287-
return 'get' . Str::ucfirst($property);
286+
return 'get'.Str::ucfirst($property);
288287
}, $properties), true);
289288
})
290289
->reduce(function ($carry, ReflectionMethod $method) use ($feed, $delimiter) {
291290
if ($method->getName() === 'getFeedItems') {
292-
return $carry . $delimiter . $method->invoke($feed)->reduce(function ($carry, FeedItem $feedItem) use ($delimiter) {
293-
return $carry . $delimiter . self::generateChecksumForFeedItem($feedItem);
294-
});
291+
return $carry.$delimiter.$method->invoke($feed)->reduce(function ($carry, FeedItem $feedItem) use ($delimiter) {
292+
return $carry.$delimiter.self::generateChecksumForFeedItem($feedItem);
293+
});
295294
}
296295

297-
return $carry . $delimiter . $method->invoke($feed);
296+
return $carry.$delimiter.$method->invoke($feed);
298297
}, ''), $delimiter);
299298

300299
return Hash::hash($algo, $allValuesConcatenated);
301300
}
302301

303302
/**
304-
* @param callable $callback
305-
* @return mixed
306303
* @throws Exception
307304
*/
308305
private function withRetries(callable $callback): mixed

0 commit comments

Comments
 (0)