-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from struggle-for-php/0.2.x
0.2.x merge
- Loading branch information
Showing
8 changed files
with
271 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $config): void { | ||
$services = $config->services(); | ||
|
||
$config->phpVersion(\Rector\Core\ValueObject\PhpVersion::PHP_53); | ||
$services->set(\SfpDev\Stubs\Psr\Log\ExceptionLoggerContextTypeRector::class); | ||
|
||
$config->paths([ | ||
__DIR__ . '/../stubs-for-exception', | ||
]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $config): void { | ||
$services = $config->services(); | ||
|
||
$config->phpVersion(\Rector\Core\ValueObject\PhpVersion::PHP_53); | ||
$services->set(\SfpDev\Stubs\Psr\Log\ThrowableLoggerContextTypeRector::class); | ||
|
||
$config->paths([ | ||
__DIR__ . '/../stubs-for-throwable', | ||
]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SfpDev\Stubs\Psr\Log; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; | ||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
abstract class AbstractLoggerContextTypeRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition("", [new CodeSample('', '')]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [ | ||
Node\Stmt\Interface_::class, | ||
Node\Stmt\Class_::class, | ||
Node\Stmt\ClassMethod::class, | ||
]; | ||
} | ||
|
||
abstract public function getLoggerContextTypeTagValueNode(): GenericTagValueNode; | ||
|
||
public function refactor(Node $node) : Node | ||
{ | ||
if (! $this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace SfpDev\Stubs\Psr\Log; | ||
|
||
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; | ||
|
||
final class ExceptionLoggerContextTypeRector extends AbstractLoggerContextTypeRector | ||
{ | ||
public function getLoggerContextTypeTagValueNode(): GenericTagValueNode | ||
{ | ||
return new GenericTagValueNode('LoggerContextType = array{exception?: \Exception}'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace SfpDev\Stubs\Psr\Log; | ||
|
||
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; | ||
|
||
final class ThrowableLoggerContextTypeRector extends AbstractLoggerContextTypeRector | ||
{ | ||
public function getLoggerContextTypeTagValueNode(): GenericTagValueNode | ||
{ | ||
return new GenericTagValueNode('LoggerContextType = array{exception?: \Throwable}'); | ||
} | ||
} |