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\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
|
|
|
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;
|
2021-01-19 22:32:28 +01:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
2021-01-19 22:32:28 +01:00
|
|
|
*/
|
|
|
|
private $phpDocInfoFactory;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(ConstantNaming $constantNaming, PhpDocInfoFactory $phpDocInfoFactory)
|
2021-01-15 01:13:31 +01:00
|
|
|
{
|
|
|
|
$this->constantNaming = $constantNaming;
|
2021-01-19 22:32:28 +01:00
|
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
2021-01-15 01:13:31 +01:00
|
|
|
}
|
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;
|
2021-01-19 22:32:28 +01:00
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
|
2021-01-20 17:17:59 +01:00
|
|
|
$phpDocInfo->markAsChanged();
|
2022-06-07 08:22:29 +00:00
|
|
|
$classConst->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
|
2021-01-15 01:13:31 +01:00
|
|
|
return $classConst;
|
|
|
|
}
|
|
|
|
}
|