Skip to content

Commit ce4bf6c

Browse files
authored
Merge pull request #562 from uzulla/use-phpstan
Add PHPStan Static Analysis (Level Max + Strict Rules)
2 parents b791f12 + 9a85f3f commit ce4bf6c

File tree

99 files changed

+1346
-414
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+1346
-414
lines changed

.github/workflows/test.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,29 @@ jobs:
4242

4343
- name: Tests
4444
run: composer test
45+
46+
phpstan:
47+
name: PHPStan Static Analysis
48+
runs-on: ubuntu-latest
49+
50+
steps:
51+
- name: Checkout
52+
uses: actions/checkout@v4
53+
54+
- name: Install PHP
55+
uses: shivammathur/setup-php@v2
56+
with:
57+
php-version: 8.4
58+
59+
- name: Cache PHP dependencies
60+
uses: actions/cache@v4
61+
with:
62+
path: vendor
63+
key: ${{ runner.os }}-php-8.4-composer-${{ hashFiles('**/composer.json') }}
64+
restore-keys: ${{ runner.os }}-php-8.4-composer-
65+
66+
- name: Install dependencies
67+
run: composer install
68+
69+
- name: Run PHPStan
70+
run: composer phpstan

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
"nyholm/psr7": "^1.2",
4343
"oscarotero/php-cs-fixer-config": "^1.0",
4444
"brick/varexporter": "^0.3.1",
45-
"symfony/css-selector": "^5.0"
45+
"symfony/css-selector": "^5.0",
46+
"phpstan/phpstan": "^2.1",
47+
"phpstan/phpstan-strict-rules": "^2.0"
4648
},
4749
"suggest": {
4850
"symfony/css-selector": "If you want to get elements using css selectors"
@@ -64,6 +66,7 @@
6466
"demo": "php -S localhost:8888 demo/index.php",
6567
"test": "phpunit",
6668
"cs-fix": "php-cs-fixer fix",
69+
"phpstan": "phpstan --memory-limit=-1",
6770
"update-resources": [
6871
"php scripts/update-oembed.php",
6972
"php scripts/update-suffix.php"

phpstan.dist.neon

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
includes:
2+
- vendor/phpstan/phpstan-strict-rules/rules.neon
3+
4+
parameters:
5+
level: max
6+
paths:
7+
- src
8+
# - tests
9+
excludePaths:
10+
- tests/cache
11+
- tests/fixtures
12+
checkMissingCallableSignature: true
13+
checkUninitializedProperties: true
14+
checkTooWideReturnTypesInProtectedAndPublicMethods: true
15+
checkImplicitMixed: true

src/Adapters/Archive/Api.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class Api
99
{
1010
use HttpApiTrait;
1111

12+
/**
13+
* @return array<string, mixed>
14+
*/
1215
protected function fetchData(): array
1316
{
1417
$this->endpoint = $this->extractor->getUri()->withQuery('output=json');

src/Adapters/Archive/Detectors/AuthorName.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33

44
namespace Embed\Adapters\Archive\Detectors;
55

6+
use Embed\Adapters\Archive\Extractor;
67
use Embed\Detectors\AuthorName as Detector;
78

89
class AuthorName extends Detector
910
{
1011
public function detect(): ?string
1112
{
12-
$api = $this->extractor->getApi();
13+
/** @var Extractor $extractor */
14+
$extractor = $this->extractor;
15+
$api = $extractor->getApi();
1316

14-
return $api->str('metadata', 'creator')
15-
?: parent::detect();
17+
$result = $api->str('metadata', 'creator');
18+
return (is_string($result) && trim($result) !== '') ? $result : parent::detect();
1619
}
1720
}

src/Adapters/Archive/Detectors/Description.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33

44
namespace Embed\Adapters\Archive\Detectors;
55

6+
use Embed\Adapters\Archive\Extractor;
67
use Embed\Detectors\Description as Detector;
78

89
class Description extends Detector
910
{
1011
public function detect(): ?string
1112
{
12-
$api = $this->extractor->getApi();
13+
/** @var Extractor $extractor */
14+
$extractor = $this->extractor;
15+
$api = $extractor->getApi();
1316

14-
return $api->str('metadata', 'extract')
15-
?: parent::detect();
17+
$result = $api->str('metadata', 'extract');
18+
return (is_string($result) && trim($result) !== '') ? $result : parent::detect();
1619
}
1720
}

src/Adapters/Archive/Detectors/PublishedTime.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44
namespace Embed\Adapters\Archive\Detectors;
55

66
use DateTime;
7+
use Embed\Adapters\Archive\Extractor;
78
use Embed\Detectors\PublishedTime as Detector;
89

910
class PublishedTime extends Detector
1011
{
1112
public function detect(): ?DateTime
1213
{
13-
$api = $this->extractor->getApi();
14+
/** @var Extractor $extractor */
15+
$extractor = $this->extractor;
16+
$api = $extractor->getApi();
1417

15-
return $api->time('metadata', 'publicdate')
16-
?: $api->time('metadata', 'addeddate')
17-
?: $api->time('metadata', 'date')
18-
?: parent::detect();
18+
$fields = ['publicdate', 'addeddate', 'date'];
19+
foreach ($fields as $field) {
20+
$result = $api->time('metadata', $field);
21+
if ($result !== null) {
22+
return $result;
23+
}
24+
}
25+
26+
return parent::detect();
1927
}
2028
}

src/Adapters/Archive/Detectors/Title.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33

44
namespace Embed\Adapters\Archive\Detectors;
55

6+
use Embed\Adapters\Archive\Extractor;
67
use Embed\Detectors\Title as Detector;
78

89
class Title extends Detector
910
{
1011
public function detect(): ?string
1112
{
12-
$api = $this->extractor->getApi();
13+
/** @var Extractor $extractor */
14+
$extractor = $this->extractor;
15+
$api = $extractor->getApi();
1316

14-
return $api->str('metadata', 'title')
15-
?: parent::detect();
17+
$result = $api->str('metadata', 'title');
18+
return (is_string($result) && trim($result) !== '') ? $result : parent::detect();
1619
}
1720
}

src/Adapters/Archive/Extractor.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,35 @@
44
namespace Embed\Adapters\Archive;
55

66
use Embed\Extractor as Base;
7+
use Embed\Http\Crawler;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Message\UriInterface;
711

812
class Extractor extends Base
913
{
1014
private Api $api;
1115

16+
public function __construct(
17+
UriInterface $uri,
18+
RequestInterface $request,
19+
ResponseInterface $response,
20+
Crawler $crawler
21+
) {
22+
parent::__construct($uri, $request, $response, $crawler);
23+
$this->api = new Api($this);
24+
}
25+
1226
public function getApi(): Api
1327
{
1428
return $this->api;
1529
}
1630

31+
/**
32+
* @return array<string, \Embed\Detectors\Detector>
33+
*/
1734
public function createCustomDetectors(): array
1835
{
19-
$this->api = new Api($this);
20-
2136
return [
2237
'title' => new Detectors\Title($this),
2338
'description' => new Detectors\Description($this),

src/Adapters/Bandcamp/Extractor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
class Extractor extends Base
99
{
10+
/**
11+
* @return array<string, \Embed\Detectors\Detector>
12+
*/
1013
public function createCustomDetectors(): array
1114
{
1215
return [

0 commit comments

Comments
 (0)