2021-01-15 01:13:31 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Privatization\NodeFactory;
|
2021-01-15 01:13:31 +01:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Const_;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassConst;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
|
|
use Rector\Privatization\Naming\ConstantNaming;
|
2021-01-15 01:13:31 +01:00
|
|
|
final class ClassConstantFactory
|
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Privatization\Naming\ConstantNaming
|
2021-01-15 01:13:31 +01:00
|
|
|
*/
|
|
|
|
private $constantNaming;
|
2022-12-16 00:43:16 +00:00
|
|
|
public function __construct(ConstantNaming $constantNaming)
|
2021-01-15 01:13:31 +01:00
|
|
|
{
|
|
|
|
$this->constantNaming = $constantNaming;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function createFromProperty(Property $property) : ClassConst
|
2021-01-15 01:13:31 +01:00
|
|
|
{
|
|
|
|
$propertyProperty = $property->props[0];
|
|
|
|
$constantName = $this->constantNaming->createFromProperty($propertyProperty);
|
|
|
|
/** @var Expr $defaultValue */
|
|
|
|
$defaultValue = $propertyProperty->default;
|
2022-06-07 08:22:29 +00:00
|
|
|
$const = new Const_($constantName, $defaultValue);
|
|
|
|
$classConst = new ClassConst([$const]);
|
|
|
|
$classConst->flags = $property->flags & ~Class_::MODIFIER_STATIC;
|
2022-07-21 21:45:25 +00:00
|
|
|
$const->setAttribute(AttributeKey::PARENT_NODE, $classConst);
|
2022-12-16 00:43:16 +00:00
|
|
|
$classConst->setAttribute(AttributeKey::PHP_DOC_INFO, $property->getAttribute(AttributeKey::PHP_DOC_INFO));
|
|
|
|
$classConst->setAttribute(AttributeKey::COMMENTS, $property->getAttribute(AttributeKey::COMMENTS));
|
2022-07-21 21:45:25 +00:00
|
|
|
$classConst->setAttribute(AttributeKey::PARENT_NODE, $property->getAttribute(AttributeKey::PARENT_NODE));
|
2021-01-15 01:13:31 +01:00
|
|
|
return $classConst;
|
|
|
|
}
|
|
|
|
}
|