diff --git a/build/rector-stubs-for-exception.php b/build/rector-stubs-for-exception.php new file mode 100644 index 0000000..beb2d8c --- /dev/null +++ b/build/rector-stubs-for-exception.php @@ -0,0 +1,14 @@ +services(); + + $config->phpVersion(\Rector\Core\ValueObject\PhpVersion::PHP_53); + $services->set(\SfpDev\Stubs\Psr\Log\ExceptionLoggerContextTypeRector::class); + + $config->paths([ + __DIR__ . '/../stubs-for-exception', + ]); +}; \ No newline at end of file diff --git a/build/rector-stubs-for-throwable.php b/build/rector-stubs-for-throwable.php new file mode 100644 index 0000000..2cf59d5 --- /dev/null +++ b/build/rector-stubs-for-throwable.php @@ -0,0 +1,14 @@ +services(); + + $config->phpVersion(\Rector\Core\ValueObject\PhpVersion::PHP_53); + $services->set(\SfpDev\Stubs\Psr\Log\ThrowableLoggerContextTypeRector::class); + + $config->paths([ + __DIR__ . '/../stubs-for-throwable', + ]); +}; \ No newline at end of file diff --git a/build/transform-source b/build/transform-source new file mode 100644 index 0000000..cb6c6f0 --- /dev/null +++ b/build/transform-source @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +## stubs-for-exception +cp vendor/psr/log/Psr/Log/LoggerInterface.php stubs-for-exception/LoggerInterface.php +cp vendor/psr/log/Psr/Log/AbstractLogger.php stubs-for-exception/AbstractLogger.php +vendor/bin/rector process --config=build/rector-stubs-for-exception.php --clear-cache --no-diffs +mv stubs-for-exception/LoggerInterface.php stubs-for-exception/LoggerInterface.phpstub +mv stubs-for-exception/AbstractLogger.php stubs-for-exception/AbstractLogger.phpstub + +## stubs-for-throwable +cp vendor/psr/log/Psr/Log/LoggerInterface.php stubs-for-throwable/LoggerInterface.php +cp vendor/psr/log/Psr/Log/AbstractLogger.php stubs-for-throwable/AbstractLogger.php +vendor/bin/rector process --config=build/rector-stubs-for-throwable.php --clear-cache --no-diffs +mv stubs-for-throwable/LoggerInterface.php stubs-for-throwable/LoggerInterface.phpstub +mv stubs-for-throwable/AbstractLogger.php stubs-for-throwable/AbstractLogger.phpstub diff --git a/composer.json b/composer.json index ad7fda8..8751986 100644 --- a/composer.json +++ b/composer.json @@ -16,12 +16,14 @@ "require-dev": { "ext-simplexml": "*", "psr/log": "^1", + "rector/rector": "0.15.21", "squizlabs/php_codesniffer": "^3.7", "vimeo/psalm": "^5", "weirdan/codeception-psalm-module": "^0.14.0" }, "autoload-dev": { "psr-4": { + "SfpDev\\Stubs\\Psr\\Log\\": "src-dev", "SfpTest\\Stubs\\Psr\\Log\\": "tests" } }, diff --git a/composer.lock b/composer.lock index 094b0d2..0c45826 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8c843295438a0840dbe3a5aaad7fb1a2", + "content-hash": "e9edd8d396c3165dab2f3854c641f2d3", "packages": [], "packages-dev": [ { @@ -1771,6 +1771,68 @@ }, "time": "2023-02-07T18:11:17+00:00" }, + { + "name": "phpstan/phpstan", + "version": "1.10.8", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "0166aef76e066f0dd2adc2799bdadfa1635711e9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/0166aef76e066f0dd2adc2799bdadfa1635711e9", + "reference": "0166aef76e066f0dd2adc2799bdadfa1635711e9", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2023-03-24T10:28:16+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "9.2.26", @@ -2491,6 +2553,67 @@ }, "time": "2019-03-08T08:55:37+00:00" }, + { + "name": "rector/rector", + "version": "0.15.21", + "source": { + "type": "git", + "url": "https://github.com/rectorphp/rector.git", + "reference": "1cee8cc5d6d836e1bf9a3006d7b062adde3a6022" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rectorphp/rector/zipball/1cee8cc5d6d836e1bf9a3006d7b062adde3a6022", + "reference": "1cee8cc5d6d836e1bf9a3006d7b062adde3a6022", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0", + "phpstan/phpstan": "^1.10.1" + }, + "conflict": { + "rector/rector-doctrine": "*", + "rector/rector-downgrade-php": "*", + "rector/rector-phpunit": "*", + "rector/rector-symfony": "*" + }, + "bin": [ + "bin/rector" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "0.15-dev" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Instant Upgrade and Automated Refactoring of any PHP code", + "keywords": [ + "automation", + "dev", + "migration", + "refactoring" + ], + "support": { + "issues": "https://github.com/rectorphp/rector/issues", + "source": "https://github.com/rectorphp/rector/tree/0.15.21" + }, + "funding": [ + { + "url": "https://github.com/tomasvotruba", + "type": "github" + } + ], + "time": "2023-03-06T11:44:29+00:00" + }, { "name": "sebastian/cli-parser", "version": "1.0.1", diff --git a/src-dev/AbstractLoggerContextTypeRector.php b/src-dev/AbstractLoggerContextTypeRector.php new file mode 100644 index 0000000..e410e70 --- /dev/null +++ b/src-dev/AbstractLoggerContextTypeRector.php @@ -0,0 +1,76 @@ +isObjectType($node, new \PHPStan\Type\ObjectType('Psr\Log\LoggerInterface'))) { + return $node; + } + + if ($node instanceof Node\Stmt\Interface_) { + $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); + $phpDocInfo->addPhpDocTagNode( + new PhpDocTagNode('@psalm-type', $this->getLoggerContextTypeTagValueNode()) + ); + + return $node; + } + + if ($node instanceof Node\Stmt\Class_ && $node->name->name === 'AbstractLogger') { + $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); + $phpDocInfo->addPhpDocTagNode( + new PhpDocTagNode('@psalm-import-type', new GenericTagValueNode('LoggerContextType from LoggerInterface')) + ); + + return $node; + } + + + if ($node instanceof Node\Stmt\ClassMethod) { + $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); + $context = $phpDocInfo->getParamTagValueByName('context'); + assert($context instanceof ParamTagValueNode); + $context->type = new IdentifierTypeNode('LoggerContextType'); + + if ($node->name->name === 'log') { + $level = $phpDocInfo->getParamTagValueByName('level'); + assert($level instanceof ParamTagValueNode); + $level->type = new IdentifierTypeNode('LogLevel::*'); + } + + return $node; + } + + return $node; + } +} \ No newline at end of file diff --git a/src-dev/ExceptionLoggerContextTypeRector.php b/src-dev/ExceptionLoggerContextTypeRector.php new file mode 100644 index 0000000..f99fe46 --- /dev/null +++ b/src-dev/ExceptionLoggerContextTypeRector.php @@ -0,0 +1,13 @@ +