Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wire up PHPStan #95

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']

runs-on: ${{ matrix.operating-system }}

Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
on:
pull_request:
push:
branches:
- master

name: phpstan
jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.2'

- name: Install dependencies with composer
run: composer install

- name: Cache phpstan cache
uses: actions/cache@v4
with:
path: .phpstan-cache
key: phpstan-cache-${{ github.run_id }} # see https://github.com/phpstan/phpstan/discussions/9301
restore-keys: |
phpstan-cache-

- name: PHPStan
run: make phpstan
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ phpunit.xml
tests/user_agents.json

*.cache
/.phpstan-cache
4 changes: 3 additions & 1 deletion .helpers/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

require __DIR__ . '/../vendor/autoload.php';

// @phpstan-ignore argument.type
$class = new ReflectionClass($argv[1]);

echo "Predefined helper constants from `{$class->getName()}`\n\n";

echo "| Constant | {$argv[2]} | \n|----------|----------| \n";

foreach( $class->getConstants() as $constant => $value ) {
assert(is_string($value));
echo "| `{$class->getShortName()}::{$constant}` | {$value} | \n";
}

echo "\n";
echo "\n";
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ generate:
init:
php bin/init_user_agent.php > tests/user_agents.tmp.json && mv tests/user_agents.tmp.json tests/user_agents.dist.json
make generate

.PHONY: phpstan
phpstan:
php vendor/bin/phpstan analyse --memory-limit 2g
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ All that said, there is the start of a [branch to do it](https://github.com/dona

## Requirements

- **php**: >=5.4.0
- **php**: >=7.2.0
- **ext-ctype**: *

## Installing
Expand Down
10 changes: 8 additions & 2 deletions bin/benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
require __DIR__ . '/../src/UserAgentParser.php';

$time = microtime(true);
$jsonfile = __DIR__ . '/../tests/user_agents.dist.json';
$content = file_get_contents($jsonfile);
if( $content === false ) {
echo "Failed to read file: $jsonfile\n";
exit(1);
}

$uas = json_decode(file_get_contents(__DIR__ . '/../tests/user_agents.dist.json'), true);

$uas = json_decode($content, true);
assert(is_array($uas));

foreach( $uas as $ua => $junk ) {
$uatime = microtime(true);
Expand Down
15 changes: 11 additions & 4 deletions bin/constant_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
require __DIR__ . '/../vendor/autoload.php';

$jsonfile = __DIR__ . '/../tests/user_agents.dist.json';
$content = file_get_contents($jsonfile);
if( $content === false ) {
echo "Failed to read file: $jsonfile\n";
die(1);
}

$uas = json_decode(
file_get_contents($jsonfile),
$content,
true
);

assert(is_array($uas));

$platforms = [];
$browsers = [];
foreach( $uas as $key => $val ) {
Expand Down Expand Up @@ -55,7 +62,7 @@
$browserBody = "{$header}namespace donatj\UserAgent;\n\ninterface Browsers {\n\n";
$maxKey = max(array_map('strlen', array_keys($browsers)));
foreach( $browsers as $const => $val ) {
$browserBody .= sprintf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true));
$browserBody .= sprintf("\tpublic const %-{$maxKey}s = %s;\n", $const, var_export(key($val), true));
}
$browserBody .= "\n}\n\n";

Expand All @@ -69,9 +76,9 @@
$platformBody = "{$header}namespace donatj\UserAgent;\n\ninterface Platforms {\n\n";
$maxKey = max(array_map('strlen', array_keys($platforms)));
foreach( $platforms as $const => $val ) {
$platformBody .= sprintf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true));
$platformBody .= sprintf("\tpublic const %-{$maxKey}s = %s;\n", $const, var_export(key($val), true));
}
$platformBody .= "\n}\n\n";

file_put_contents(__DIR__ . '/../src/UserAgent/Browsers.php', $browserBody);
file_put_contents(__DIR__ . '/../src/UserAgent/Platforms.php', $platformBody);
file_put_contents(__DIR__ . '/../src/UserAgent/Platforms.php', $platformBody);
9 changes: 8 additions & 1 deletion bin/init_user_agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

$jsonfile = __DIR__ . '/../Tests/user_agents.dist.json';

$uas = json_decode(file_get_contents($jsonfile), true);
$content = file_get_contents($jsonfile);
if( $content === false ) {
echo "Failed to read file: $jsonfile\n";
exit(1);
}

$uas = json_decode($content, true);
assert(is_array($uas));

foreach( $uas as $key => &$val ) {
$val = parse_user_agent($key);
Expand Down
18 changes: 15 additions & 3 deletions bin/user_agent_sorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

$jsonfile = __DIR__ . '/../Tests/user_agents.dist.json';

$uas = json_decode(file_get_contents($jsonfile), true);
$content = file_get_contents($jsonfile);
if( $content === false ) {
echo "Failed to read file: $jsonfile\n";
exit(1);
}

$uas = json_decode($content, true);
assert(is_array($uas));

foreach( $uas as $key => &$val ) {
$val['key'] = $key;
Expand Down Expand Up @@ -73,6 +80,11 @@
echo $json;


/**
* @param string $a
* @param string $b
* @return 0|1|-1
*/
function compare_version( $a, $b ) {
$cmp_a = explode('.', $a);
$cmp_b = explode('.', $b);
Expand All @@ -91,10 +103,10 @@ function compare_version( $a, $b ) {
}

if( $cmp = strcmp($aa, $bb) ) {
$value = $cmp / abs($cmp);
$value = intval($cmp / abs($cmp));
break;
}
}

return $value;
}
}
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
"homepage": "https://donatstudios.com/PHP-Parser-HTTP_USER_AGENT",
"license": "MIT",
"require": {
"php": ">=5.4.0",
"php": ">=7.2.0",
"ext-ctype": "*"
},
"require-dev": {
"camspiers/json-pretty": "~1.0",
"phpunit/phpunit": "~4.8|~9",
"donatj/drop": "*",
"ext-json": "*"
"ext-json": "*",
"phpstan/phpstan": "^1.11"
},
"authors": [
{
Expand Down
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
level: 9
tmpDir: .phpstan-cache
paths:
- src
- bin
- examples
- .helpers
phpVersion: 70200
102 changes: 51 additions & 51 deletions src/UserAgent/Browsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,57 @@

interface Browsers {

const ADSBOT_GOOGLE = 'AdsBot-Google';
const ANDROID_BROWSER = 'Android Browser';
const APPLEBOT = 'Applebot';
const BAIDUSPIDER = 'Baiduspider';
const BINGBOT = 'bingbot';
const BLACKBERRY_BROWSER = 'BlackBerry Browser';
const BROWSER = 'Browser';
const BUNJALLOO = 'Bunjalloo';
const CAMINO = 'Camino';
const CHATGPT_USER = 'ChatGPT-User';
const CHROME = 'Chrome';
const CURL = 'curl';
const EDGE = 'Edge';
const FACEBOOKEXTERNALHIT = 'facebookexternalhit';
const FEEDVALIDATOR = 'FeedValidator';
const FIREFOX = 'Firefox';
const GOOGLEBOT = 'Googlebot';
const GOOGLEBOT_IMAGE = 'Googlebot-Image';
const GOOGLEBOT_VIDEO = 'Googlebot-Video';
const GPTBOT = 'GPTBot';
const HEADLESSCHROME = 'HeadlessChrome';
const IEMOBILE = 'IEMobile';
const IMESSAGEBOT = 'iMessageBot';
const KINDLE = 'Kindle';
const LYNX = 'Lynx';
const MASTODON = 'Mastodon';
const MIDORI = 'Midori';
const MIUIBROWSER = 'MiuiBrowser';
const MSIE = 'MSIE';
const MSNBOT_MEDIA = 'msnbot-media';
const NETFRONT = 'NetFront';
const NINTENDOBROWSER = 'NintendoBrowser';
const OAI_SEARCHBOT = 'OAI-SearchBot';
const OCULUSBROWSER = 'OculusBrowser';
const OPERA = 'Opera';
const PUFFIN = 'Puffin';
const SAFARI = 'Safari';
const SAILFISHBROWSER = 'SailfishBrowser';
const SAMSUNGBROWSER = 'SamsungBrowser';
const SILK = 'Silk';
const SLACKBOT = 'Slackbot';
const TELEGRAMBOT = 'TelegramBot';
const TIZENBROWSER = 'TizenBrowser';
const TWITTERBOT = 'Twitterbot';
const UC_BROWSER = 'UC Browser';
const VALVE_STEAM_TENFOOT = 'Valve Steam Tenfoot';
const VIVALDI = 'Vivaldi';
const WGET = 'Wget';
const WORDPRESS = 'WordPress';
const YANDEX = 'Yandex';
const YANDEXBOT = 'YandexBot';
public const ADSBOT_GOOGLE = 'AdsBot-Google';
public const ANDROID_BROWSER = 'Android Browser';
public const APPLEBOT = 'Applebot';
public const BAIDUSPIDER = 'Baiduspider';
public const BINGBOT = 'bingbot';
public const BLACKBERRY_BROWSER = 'BlackBerry Browser';
public const BROWSER = 'Browser';
public const BUNJALLOO = 'Bunjalloo';
public const CAMINO = 'Camino';
public const CHATGPT_USER = 'ChatGPT-User';
public const CHROME = 'Chrome';
public const CURL = 'curl';
public const EDGE = 'Edge';
public const FACEBOOKEXTERNALHIT = 'facebookexternalhit';
public const FEEDVALIDATOR = 'FeedValidator';
public const FIREFOX = 'Firefox';
public const GOOGLEBOT = 'Googlebot';
public const GOOGLEBOT_IMAGE = 'Googlebot-Image';
public const GOOGLEBOT_VIDEO = 'Googlebot-Video';
public const GPTBOT = 'GPTBot';
public const HEADLESSCHROME = 'HeadlessChrome';
public const IEMOBILE = 'IEMobile';
public const IMESSAGEBOT = 'iMessageBot';
public const KINDLE = 'Kindle';
public const LYNX = 'Lynx';
public const MASTODON = 'Mastodon';
public const MIDORI = 'Midori';
public const MIUIBROWSER = 'MiuiBrowser';
public const MSIE = 'MSIE';
public const MSNBOT_MEDIA = 'msnbot-media';
public const NETFRONT = 'NetFront';
public const NINTENDOBROWSER = 'NintendoBrowser';
public const OAI_SEARCHBOT = 'OAI-SearchBot';
public const OCULUSBROWSER = 'OculusBrowser';
public const OPERA = 'Opera';
public const PUFFIN = 'Puffin';
public const SAFARI = 'Safari';
public const SAILFISHBROWSER = 'SailfishBrowser';
public const SAMSUNGBROWSER = 'SamsungBrowser';
public const SILK = 'Silk';
public const SLACKBOT = 'Slackbot';
public const TELEGRAMBOT = 'TelegramBot';
public const TIZENBROWSER = 'TizenBrowser';
public const TWITTERBOT = 'Twitterbot';
public const UC_BROWSER = 'UC Browser';
public const VALVE_STEAM_TENFOOT = 'Valve Steam Tenfoot';
public const VIVALDI = 'Vivaldi';
public const WGET = 'Wget';
public const WORDPRESS = 'WordPress';
public const YANDEX = 'Yandex';
public const YANDEXBOT = 'YandexBot';

}

Loading