mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-15 13:25:30 +01:00
55 lines
1.5 KiB
PHP
55 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Rector\Privatization\NodeFactory;
|
|
|
|
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\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
use Rector\Privatization\Naming\ConstantNaming;
|
|
|
|
final class ClassConstantFactory
|
|
{
|
|
/**
|
|
* @var ConstantNaming
|
|
*/
|
|
private $constantNaming;
|
|
|
|
/**
|
|
* @var PhpDocInfoFactory
|
|
*/
|
|
private $phpDocInfoFactory;
|
|
|
|
public function __construct(ConstantNaming $constantNaming, PhpDocInfoFactory $phpDocInfoFactory)
|
|
{
|
|
$this->constantNaming = $constantNaming;
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
|
}
|
|
|
|
public function createFromProperty(Property $property): ClassConst
|
|
{
|
|
$propertyProperty = $property->props[0];
|
|
|
|
$constantName = $this->constantNaming->createFromProperty($propertyProperty);
|
|
|
|
/** @var Expr $defaultValue */
|
|
$defaultValue = $propertyProperty->default;
|
|
$const = new Const_($constantName, $defaultValue);
|
|
|
|
$classConst = new ClassConst([$const]);
|
|
$classConst->flags = $property->flags & ~ Class_::MODIFIER_STATIC;
|
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
|
|
$phpDocInfo->markAsChanged();
|
|
|
|
$classConst->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
|
|
|
|
return $classConst;
|
|
}
|
|
}
|