Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,14 @@ parameters:
paths:
- %currentWorkingDirectory%/tests/Extractor/HttpClientTest.php
- %currentWorkingDirectory%/tests/SiteConfig/ConfigBuilderTest.php
-
identifier: argument.type
path: %currentWorkingDirectory%/src/Extractor/ContentExtractor.php
count: 18
-
identifier: property.notFound
path: %currentWorkingDirectory%/src/Extractor/ContentExtractor.php
count: 5

inferPrivatePropertyTypeFromConstructor: true
treatPhpDocTypesAsCertain: false
20 changes: 10 additions & 10 deletions src/Extractor/ContentExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
class ContentExtractor
{
private readonly ContentExtractorConfig $config;
private LoggerInterface $logger;
private readonly ConfigBuilder $configBuilder;

/**
Expand All @@ -39,11 +38,12 @@ class ContentExtractor
* json_ld_ignore_types?: string[],
* } $config
*/
public function __construct(array $config = [], ?LoggerInterface $logger = null, ?ConfigBuilder $configBuilder = null)
{
public function __construct(
array $config = [],
private ?LoggerInterface $logger = new NullLogger(),
?ConfigBuilder $configBuilder = null
) {
$this->config = new ContentExtractorConfig($config);

$this->logger = $logger ?? new NullLogger();
$this->configBuilder = $configBuilder ?? new ConfigBuilder($this->config->getConfigBuilder(), $this->logger);
}

Expand Down Expand Up @@ -488,7 +488,7 @@ public function process(string $html, UriInterface $url, ?SiteConfig $siteConfig
$readability->dom,
'Author found (rel="author"): {author}',
$xpath,
fn ($element, $currentEntity) => $currentEntity + [trim((string) $element)]
static fn ($element, $currentEntity) => $currentEntity + [trim((string) $element)]
);
if (null !== $extractedAuthors) {
$authors = array_merge($authors, $extractedAuthors);
Expand All @@ -501,7 +501,7 @@ public function process(string $html, UriInterface $url, ?SiteConfig $siteConfig
$readability->dom,
'Author found (meta name="author"): {author}',
$xpath,
fn ($element, $currentEntity) => $currentEntity + [trim((string) $element)]
static fn ($element, $currentEntity) => $currentEntity + [trim((string) $element)]
);
if (null !== $extractedAuthors) {
$authors = array_merge($authors, $extractedAuthors);
Expand Down Expand Up @@ -846,7 +846,7 @@ private function extractEntityFromQuery(string $entity, bool $detectEntity, stri

// we define the default callback here
if (null === $returnCallback) {
$returnCallback = fn ($element) => trim((string) $element);
$returnCallback = static fn ($element) => trim((string) $element);
}

// check for given css class
Expand Down Expand Up @@ -1101,7 +1101,7 @@ private function extractEntityFromPattern(string $entity, string $pattern, \DOMX
{
// we define the default callback here
if (null === $returnCallback) {
$returnCallback = fn ($e) => trim((string) $e);
$returnCallback = static fn ($e) => trim((string) $e);
}

$elems = $xpath->evaluate($pattern, $readability->dom);
Expand Down Expand Up @@ -1148,7 +1148,7 @@ private function extractMultipleEntityFromPattern(string $entity, string $patter
{
// we define the default callback here
if (null === $returnCallback) {
$returnCallback = fn ($e) => trim((string) $e);
$returnCallback = static fn ($e) => trim((string) $e);
}

$elems = $xpath->evaluate($pattern, $readability->dom);
Expand Down
2 changes: 1 addition & 1 deletion src/Extractor/ContentExtractorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function __construct(array $config)
$resolver->setAllowedTypes('src_lazy_load_attributes', 'string[]');
$resolver->setAllowedTypes('json_ld_ignore_types', 'string[]');

$resolver->setDefault('readability', function (OptionsResolver $readabilityResolver): void {
$resolver->setDefault('readability', static function (OptionsResolver $readabilityResolver): void {
$readabilityResolver->setDefaults([
'pre_filters' => [],
'post_filters' => [],
Expand Down
6 changes: 3 additions & 3 deletions src/Extractor/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private function getCookie(UriInterface $url, array $httpHeader = []): ?string
$this->logger->info('Found cookie "{cookie}" for url "{url}" from site config', ['cookie' => $httpHeader['cookie'], 'url' => (string) $url]);

$cookies = [];
$pieces = array_filter(array_map('trim', explode(';', $httpHeader['cookie'])));
$pieces = array_filter(array_map(trim(...), explode(';', $httpHeader['cookie'])));

foreach ($pieces as $part) {
$cookieParts = explode('=', $part, 2);
Expand All @@ -382,7 +382,7 @@ private function getCookie(UriInterface $url, array $httpHeader = []): ?string
}

// see https://tools.ietf.org/html/rfc6265.html#section-4.2.1
return implode('; ', array_map(fn ($name) => $name . '=' . $cookies[$name], array_keys($cookies)));
return implode('; ', array_map(static fn ($name) => $name . '=' . $cookies[$name], array_keys($cookies)));
}

return null;
Expand Down Expand Up @@ -515,7 +515,7 @@ private function removeTrackersFromUrl(UriInterface $uri): UriInterface
// Remove utm_* parameters
$clean_query = array_filter(
$q_array,
fn (string $param): bool => !str_starts_with($param, 'utm_')
static fn (string $param): bool => !str_starts_with($param, 'utm_')
);
$uri = $uri->withQuery(implode('&', $clean_query));
}
Expand Down
2 changes: 1 addition & 1 deletion src/GrabyConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function __construct(array $config)
$resolver->setAllowedTypes('http_client', 'array');
$resolver->setAllowedTypes('extractor', 'array');

$resolver->setNormalizer('content_type_exc', function (Options $options, $value) {
$resolver->setNormalizer('content_type_exc', static function (Options $options, $value) {
$resolver = new OptionsResolver();
$resolver->setRequired(['action', 'name']);
$resolver->setAllowedValues('action', ['link', 'exclude']);
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClient/Plugin/CookiePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
*/
private function createCookie(RequestInterface $request, string $setCookieHeader): ?Cookie
{
$parts = array_map('trim', explode(';', $setCookieHeader));
$parts = array_map(trim(...), explode(';', $setCookieHeader));

if ('' === $parts[0] || !str_contains($parts[0], '=')) {
return null;
Expand Down
9 changes: 4 additions & 5 deletions src/SiteConfig/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

class ConfigBuilder
{
private LoggerInterface $logger;
private readonly ConfigBuilderConfig $config;
/** @var array<string, string> */
private array $configFiles = [];
Expand Down Expand Up @@ -43,12 +42,12 @@ class ConfigBuilder
* hostname_regex?: string,
* } $config
*/
public function __construct(array $config = [], ?LoggerInterface $logger = null)
{
public function __construct(
array $config = [],
private ?LoggerInterface $logger = new NullLogger(),
) {
$this->config = new ConfigBuilderConfig($config);

$this->logger = $logger ?? new NullLogger();

$this->loadConfigFiles();
}

Expand Down
2 changes: 1 addition & 1 deletion src/SiteConfig/ConfigBuilderConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(array $config)
$resolver->setAllowedTypes('site_config', 'string[]');
$resolver->setAllowedTypes('hostname_regex', 'string');

$resolver->setNormalizer('site_config', function (Options $options, $value) {
$resolver->setNormalizer('site_config', static function (Options $options, $value) {
foreach ($value as $key => $path) {
$value[$key] = rtrim($path, '/');
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Extractor/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ public function testNbRedirectsReached(): void
$httpMockClient->addResponse(new Response(
308,
[
'Location' => 'http://fr.wikipedia.org/wiki/Copyright?' . random_int(0, mt_getrandmax()),
'Location' => 'http://fr.wikipedia.org/wiki/Copyright?' . random_int(0, getrandmax()),
],
'<meta HTTP-EQUIV="REFRESH" content="0; url=http://fr.wikipedia.org/wiki/Copyright?' . random_int(0, mt_getrandmax()) . '">'
'<meta HTTP-EQUIV="REFRESH" content="0; url=http://fr.wikipedia.org/wiki/Copyright?' . random_int(0, getrandmax()) . '">'
));
}

Expand Down
Loading