Skip to content

Commit

Permalink
Upgrading repo tools (#196)
Browse files Browse the repository at this point in the history
* Upgrade tooling

* Update workflow

* Drop PHP7.4 support

* Round 1 of rector

* Round 2 of rector

* Tweak rules for current state

* Upgrading PHP for tools

* Drop pint as reg. requirement

* Tweak composer requirements

* 8.1?

* update rector

* tweak command

* Less splash operators

* checkout @ v4?

* Drop PHP 8.0
  • Loading branch information
spekulatius authored Nov 28, 2023
1 parent ce39770 commit cc448fe
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 130 deletions.
45 changes: 33 additions & 12 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
name: Tests
on: [pull_request]

jobs:
Expand All @@ -7,30 +6,30 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: [8.0, 8.1, 8.2, 8.3]
php-version: [8.1, 8.2, 8.3]
composer-flags: [null]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: none
extensions: intl curl
- run: php -v
- run: composer update ${{ matrix.composer-flags }} --no-interaction --no-progress --prefer-dist --ansi
- run: ./vendor/bin/phpunit --color=always
- run: composer test:unit

## PHPSTAN
phpstan:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@2.21.0
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: '8.1'
coverage: none
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -40,19 +39,41 @@ jobs:
run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader

- name: PHPStan tests
run: composer phpstan
run: composer test:types

## RECTOR
rector:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
coverage: none
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
update: true

- name: Install dependencies
run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader

- name: PHPStan tests
run: composer test:refactor

## PINT
pint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@2.21.0
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: '8.1'
coverage: none
tools: cs2pr
env:
Expand All @@ -63,4 +84,4 @@ jobs:
run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader

- name: Run Pint
run: composer exec -- pint --test --format=checkstyle | cs2pr
run: ./vendor/bin/pint --test --format=checkstyle | cs2pr
26 changes: 19 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"ext-intl": "*",
"symfony/dom-crawler": "^5.4 || ^6.0",
"donatello-za/rake-php-plus": "^1.0.15",
"league/uri": "^6.0",
"symfony/browser-kit": "^6.0",
"symfony/http-client": "^6.0",
"symfony/css-selector": "^6.0",
"laravel/pint": "^1.5"
"symfony/css-selector": "^6.0"
},
"require-dev": {
"symfony/thanks": "^1.0.0",
"phpunit/phpunit": "^8.0.0|^9.0.0",
"illuminate/collections": "^8.0.0|^9.0.0",
"phpstan/phpstan": "^1.10"
"laravel/pint": "^1.0",
"phpstan/phpstan": "^1.0",
"rector/rector": "^0.18",
"symfony/var-dumper": "^6.0"
},
"autoload": {
"psr-4": {
Expand All @@ -47,9 +49,18 @@
}
},
"scripts": {
"test": "./vendor/phpunit/phpunit/phpunit --cache-result --cache-result-file=.tmp/phpunit --order-by=defects --colors=always --stop-on-failure",
"phpstan": "./vendor/bin/phpstan analyse",
"pint": "./vendor/bin/pint --verbose --test"
"refactor": "./vendor/bin/rector",
"lint": "./vendor/bin/pint",
"test:refactor": "./vendor/bin/rector --dry-run",
"test:lint": "./vendor/bin/pint --test",
"test:types": "./vendor/bin/phpstan analyse --ansi src/ tests/ --level=9",
"test:unit": "./vendor/phpunit/phpunit/phpunit --cache-result --cache-result-file=.tmp/phpunit --order-by=defects --colors=always --stop-on-failure",
"test": [
"@test:refactor",
"@test:lint",
"@test:types",
"@test:unit"
]
},
"funding": [
{
Expand All @@ -62,6 +73,7 @@
}
],
"config": {
"sort-packages": true,
"allow-plugins": {
"symfony/thanks": true
}
Expand Down
25 changes: 25 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__.'/src',
]);

$rectorConfig->rules([
InlineConstructorDefaultToPropertyRector::class,
]);

$rectorConfig->sets([
// LevelSetList::UP_TO_PHP_82,
// SetList::CODE_QUALITY,
SetList::DEAD_CODE,
SetList::TYPE_DECLARATION,
]);
};
17 changes: 4 additions & 13 deletions src/DataTransferObjects/FeedEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,16 @@
*/
class FeedEntry
{
// Support for PHP7.4
public string $title;

public string $description;

public string $link;

/**
* @todo with drop of PHP7.4 we should make these public and remove the initialization above.
* @todo with drop of PHP7.4 and 8.0 we should make this `readonly`.
*/
public function __construct(
string $title,
string $description,
string $link
// Support for PHP7.4
public string $title,
public string $description,
public string $link
) {
$this->title = $title;
$this->description = $description;
$this->link = $link;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/PHPScraper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ class PHPScraper

/**
* Holds the Core class. It handles the actual scraping.
*
* @var \Spekulatius\PHPScraper\Core
*/
protected $core = null;
protected Core $core;

/**
* @param PHPScraperConfig $config
Expand Down
6 changes: 3 additions & 3 deletions src/UsesBrowserKit.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ trait UsesBrowserKit
*
* @var \Symfony\Component\BrowserKit\HttpBrowser
*/
protected $client = null;
protected $client;

/**
* Holds the HttpClient
*
* @var \Symfony\Contracts\HttpClient\HttpClientInterface;
*/
protected $httpClient = null;
protected $httpClient;

/**
* Holds the current page (a Crawler object)
*
* @var \Symfony\Component\DomCrawler\Crawler
*/
protected $currentPage = null;
protected $currentPage;

/**
* Overwrites the client
Expand Down
34 changes: 15 additions & 19 deletions src/UsesContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function baseHref(): ?string
/**
* Get the header collected as an array
*
* @return array<string, array|string|null>
* @return array{charset: mixed, contentType: mixed, viewport: mixed, canonical: mixed, csrfToken: mixed}
*/
public function headers(): array
{
Expand Down Expand Up @@ -102,6 +102,8 @@ public function description(): ?string

/**
* Get the meta collected as an array
*
* @return array{author: mixed, image: mixed, keywords: mixed, description: mixed}
*/
public function metaTags(): array
{
Expand Down Expand Up @@ -221,19 +223,15 @@ public function lists(): array
**/
public function orderedLists(): array
{
return array_values(array_filter($this->lists(), function ($list) {
return $list['type'] === 'ol';
}));
return array_values(array_filter($this->lists(), fn ($list): bool => $list['type'] === 'ol'));
}

/**
* @return array<string>
**/
public function unorderedLists(): array
{
return array_values(array_filter($this->lists(), function ($list) {
return $list['type'] === 'ul';
}));
return array_values(array_filter($this->lists(), fn ($list): bool => $list['type'] === 'ul'));
}

/**
Expand All @@ -254,9 +252,7 @@ public function cleanParagraphs(): array
{
return array_values(array_filter(
$this->paragraphs(),
function ($paragraph) {
return $paragraph !== '';
}
fn ($paragraph): bool => $paragraph !== ''
));
}

Expand Down Expand Up @@ -287,7 +283,7 @@ public function outlineWithParagraphs(): array

foreach ($result as $index => $array) {
$result[$index] = array_combine(['tag', 'content'], (array) $array);
$result[$index]['content'] = trim($result[$index]['content']);
$result[$index]['content'] = trim((string) $result[$index]['content']);
}

return $result;
Expand Down Expand Up @@ -451,7 +447,7 @@ public function internalLinks(): array
// Filter the array
return array_values(array_filter(
$this->links(),
function ($link) use (&$currentRootDomain) {
function ($link) use (&$currentRootDomain): bool {
$linkRootDomain = Uri::createFromString($link)->getHost();

return $currentRootDomain === $linkRootDomain;
Expand Down Expand Up @@ -502,18 +498,18 @@ public function linksWithDetails(): array
// Prepare the result set.
$entry = [
'url' => $uri,
'protocol' => \strpos($uri, ':') !== false ? explode(':', $uri)[0] : null,
'protocol' => str_contains($uri, ':') ? explode(':', $uri)[0] : null,
'text' => trim($link->nodeValue ?? ''),
'title' => $link->getAttribute('title') === '' ? null : $link->getAttribute('title'),
'target' => $link->getAttribute('target') === '' ? null : $link->getAttribute('target'),
'rel' => ($rel === '') ? null : strtolower($rel),
'image' => $image,
'isNofollow' => ($rel === '') ? false : (\strpos($rel, 'nofollow') !== false),
'isUGC' => ($rel === '') ? false : (\strpos($rel, 'ugc') !== false),
'isSponsored' => ($rel === '') ? false : (\strpos($rel, 'sponsored') !== false),
'isMe' => ($rel === '') ? false : (\strpos($rel, 'me') !== false),
'isNoopener' => ($rel === '') ? false : (\strpos($rel, 'noopener') !== false),
'isNoreferrer' => ($rel === '') ? false : (\strpos($rel, 'noreferrer') !== false),
'isNofollow' => ($rel === '') ? false : str_contains($rel, 'nofollow'),
'isUGC' => ($rel === '') ? false : str_contains($rel, 'ugc'),
'isSponsored' => ($rel === '') ? false : str_contains($rel, 'sponsored'),
'isMe' => ($rel === '') ? false : str_contains($rel, 'me'),
'isNoopener' => ($rel === '') ? false : str_contains($rel, 'noopener'),
'isNoreferrer' => ($rel === '') ? false : str_contains($rel, 'noreferrer'),
];

$result[] = $entry;
Expand Down
Loading

0 comments on commit cc448fe

Please sign in to comment.