From ee5a892049c12ab1601f886ebdb06352d28adc1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Olvera=20Ju=C3=A1rez=20=28jeojhx=29?= Date: Tue, 22 Feb 2022 17:35:46 -0600 Subject: [PATCH] feature: upgrade to php8.0 --- .github/workflows/code-quality.yml | 35 +++++++++++++++++ .gitignore | 2 + .php-cs-fixer.php | 63 ++++++++++++++++++++++++++++++ .php_cs | 34 ---------------- composer.json | 21 +++++++--- phpstan.neon | 5 +++ phpunit.xml.dist | 18 ++++----- src/Log.php | 20 +++++----- src/ThrowableProcessor.php | 2 +- 9 files changed, 141 insertions(+), 59 deletions(-) create mode 100644 .github/workflows/code-quality.yml create mode 100644 .php-cs-fixer.php delete mode 100644 .php_cs create mode 100644 phpstan.neon diff --git a/.github/workflows/code-quality.yml b/.github/workflows/code-quality.yml new file mode 100644 index 0000000..abf53ba --- /dev/null +++ b/.github/workflows/code-quality.yml @@ -0,0 +1,35 @@ +name: Code Quality + +on: + push: + branches: [ master ] + pull_request: ~ + +jobs: + test-php-80: + name: Code quality PHP 8.0 + runs-on: [ubuntu-latest] + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.0 + coverage: xdebug + + - name: Validate composer.json and composer.lock + run: composer validate --strict + + - name: Install dependencies + run: composer install --prefer-dist --no-progress + + - name: Lint + run: composer lint:check + + - name: PHP Static Analysis + run: composer phpstan + + - name: Unit Test + run: composer phpunit diff --git a/.gitignore b/.gitignore index ceb3215..5e4bab9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,7 @@ /.idea /bin .php_cs.cache +.php-cs-fixer.cache +.phpunit.result.cache /composer.lock /coverage.xml diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..fba9b86 --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,63 @@ +in(__DIR__ . '/src') + ->in(__DIR__ . '/tests'); + +$config = new PhpCsFixer\Config(); +$config + ->setRules([ + '@Symfony' => true, + '@PHP71Migration:risky' => true, + '@PHPUnit60Migration:risky' => true, + 'array_indentation' => true, + 'array_syntax' => ['syntax' => 'short'], + 'blank_line_after_opening_tag' => true, + 'concat_space' => ['spacing' => 'one'], + 'class_attributes_separation' => ['elements' => ['method'=>'one']], + 'declare_strict_types' => true, + 'increment_style' => ['style' => 'post'], + 'list_syntax' => ['syntax' => 'short'], + 'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'], + 'method_chaining_indentation' => true, + 'modernize_types_casting' => true, + 'multiline_whitespace_before_semicolons' => true, + 'no_superfluous_elseif' => true, + 'no_superfluous_phpdoc_tags' => false, + 'no_useless_else' => true, + 'no_useless_return' => true, + 'ordered_imports' => true, + 'phpdoc_align' => false, + 'phpdoc_order' => true, + 'php_unit_construct' => true, + 'php_unit_dedicate_assert' => true, + 'return_assignment' => true, + 'single_blank_line_at_eof' => true, + 'single_line_comment_style' => true, + 'ternary_to_null_coalescing' => true, + 'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false], + 'void_return' => true, + ]) + ->setFinder($finder) + ->setUsingCache(true) + ->setRiskyAllowed(true); +; + +// special handling of fabbot.io service if it's using too old PHP CS Fixer version +if (false !== getenv('FABBOT_IO')) { + try { + PhpCsFixer\FixerFactory::create() + ->registerBuiltInFixers() + ->registerCustomFixers($config->getCustomFixers()) + ->useRuleSet(new PhpCsFixer\RuleSet($config->getRules())) + ; + } catch (PhpCsFixer\ConfigurationException\InvalidConfigurationException $e) { + $config->setRules([]); + } catch (UnexpectedValueException $e) { + $config->setRules([]); + } catch (InvalidArgumentException $e) { + $config->setRules([]); + } +} + +return $config; diff --git a/.php_cs b/.php_cs deleted file mode 100644 index ce3eaa8..0000000 --- a/.php_cs +++ /dev/null @@ -1,34 +0,0 @@ -in(__DIR__ . '/src') - ->in(__DIR__ . '/tests'); - -return PhpCsFixer\Config::create() - ->setRules([ - '@Symfony' => true, - 'array_syntax' => ['syntax' => 'short'], - 'blank_line_after_opening_tag' => true, - 'concat_space' => ['spacing' => 'one'], - 'declare_strict_types' => true, - 'is_null' => ['use_yoda_style' => false], - 'list_syntax' => ['syntax' => 'short'], - 'method_argument_space' => ['ensure_fully_multiline' => true], - 'modernize_types_casting' => true, - 'no_multiline_whitespace_before_semicolons' => true, - 'no_useless_else' => true, - 'no_useless_return' => true, - 'ordered_imports' => true, - 'phpdoc_align' => false, - 'phpdoc_order' => true, - 'php_unit_construct' => true, - 'php_unit_dedicate_assert' => true, - 'pre_increment' => false, - 'single_line_comment_style' => true, - 'ternary_to_null_coalescing' => true, - 'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false], - 'void_return' => true, - ]) - ->setFinder($finder) - ->setUsingCache(true) - ->setRiskyAllowed(true); diff --git a/composer.json b/composer.json index 31c9ace..d8ea3a6 100644 --- a/composer.json +++ b/composer.json @@ -6,13 +6,13 @@ "homepage": "https://github.com/linioit/microlog", "license": "BSD-3-Clause", "require": { - "php": ">=7.1", - "psr/log": "^1.0", - "monolog/monolog": "^1.23" + "php": "^7.4 || ^8.0", + "psr/log": "^1.0.1 || ^2.0 || ^3.0" }, "require-dev": { - "phpunit/phpunit": "^6.0 || ^7.0", - "friendsofphp/php-cs-fixer": "^2.11" + "phpunit/phpunit": "^8.5", + "friendsofphp/php-cs-fixer": "^3.6", + "phpstan/phpstan": "^0.12" }, "autoload": { "psr-4": { @@ -23,5 +23,16 @@ "psr-4": { "Linio\\Component\\Microlog\\": "tests/" } + }, + "scripts": { + "lint": "php-cs-fixer fix --verbose --show-progress=dots", + "lint:check": "php-cs-fixer fix --dry-run --verbose --show-progress=dots", + "phpunit": "phpunit", + "phpstan": "phpstan analyze", + "test": [ + "@lint:check", + "@phpunit", + "@phpstan" + ] } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..da5b10b --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + level: max + checkMissingIterableValueType: false + paths: + - src diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a43b910..bf26a6d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,14 +1,14 @@ - + ./tests diff --git a/src/Log.php b/src/Log.php index a2620df..78e5bef 100644 --- a/src/Log.php +++ b/src/Log.php @@ -10,7 +10,7 @@ class Log { - const DEFAULT_CHANNEL = 'default'; + public const DEFAULT_CHANNEL = 'default'; /** * @var LoggerInterface[] @@ -25,7 +25,7 @@ public static function setLoggerForChannel(LoggerInterface $logger, string $chan /** * System is unusable. */ - public static function emergency($message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void + public static function emergency(string $message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void { self::log(LogLevel::EMERGENCY, $message, $context, $channel); } @@ -36,7 +36,7 @@ public static function emergency($message, array $context = [], string $channel * Example: Entire website down, database unavailable, etc. This should * trigger the SMS alerts and wake you up. */ - public static function alert($message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void + public static function alert(string $message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void { self::log(LogLevel::ALERT, $message, $context, $channel); } @@ -46,7 +46,7 @@ public static function alert($message, array $context = [], string $channel = se * * Example: Application component unavailable, unexpected exception. */ - public static function critical($message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void + public static function critical(string $message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void { self::log(LogLevel::CRITICAL, $message, $context, $channel); } @@ -55,7 +55,7 @@ public static function critical($message, array $context = [], string $channel = * Runtime errors that do not require immediate action but should typically * be logged and monitored. */ - public static function error($message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void + public static function error(string $message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void { self::log(LogLevel::ERROR, $message, $context, $channel); } @@ -66,7 +66,7 @@ public static function error($message, array $context = [], string $channel = se * Example: Use of deprecated APIs, poor use of an API, undesirable things * that are not necessarily wrong. */ - public static function warning($message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void + public static function warning(string $message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void { self::log(LogLevel::WARNING, $message, $context, $channel); } @@ -74,7 +74,7 @@ public static function warning($message, array $context = [], string $channel = /** * Normal but significant events. */ - public static function notice($message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void + public static function notice(string $message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void { self::log(LogLevel::NOTICE, $message, $context, $channel); } @@ -84,7 +84,7 @@ public static function notice($message, array $context = [], string $channel = s * * Example: User logs in, SQL logs. */ - public static function info($message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void + public static function info(string $message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void { self::log(LogLevel::INFO, $message, $context, $channel); } @@ -92,7 +92,7 @@ public static function info($message, array $context = [], string $channel = sel /** * Detailed debug information. */ - public static function debug($message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void + public static function debug(string $message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void { self::log(LogLevel::DEBUG, $message, $context, $channel); } @@ -100,7 +100,7 @@ public static function debug($message, array $context = [], string $channel = se /** * Logs with an arbitrary level. */ - public static function log($level, $message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void + public static function log(string $level, string $message, array $context = [], string $channel = self::DEFAULT_CHANNEL): void { self::getLoggerForChannel($channel)->log($level, $message, $context); } diff --git a/src/ThrowableProcessor.php b/src/ThrowableProcessor.php index f46d8c1..7e6236b 100644 --- a/src/ThrowableProcessor.php +++ b/src/ThrowableProcessor.php @@ -8,7 +8,7 @@ class ThrowableProcessor { - public function __invoke(array $record) + public function __invoke(array $record): array { if (!$record['message'] instanceof Throwable) { return $record;