builderFactory = $builderFactory;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->staticTypeMapper = $staticTypeMapper;
$this->propertyTypeDecorator = $propertyTypeDecorator;
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
}
/**
* @param string|ObjectReference::* $className
* Creates "\SomeClass::CONSTANT"
*/
public function createClassConstFetch(string $className, string $constantName) : ClassConstFetch
{
$name = $this->createName($className);
return $this->createClassConstFetchFromName($name, $constantName);
}
/**
* @param string|ObjectReference::* $className
* Creates "\SomeClass::class"
*/
public function createClassConstReference(string $className) : ClassConstFetch
{
return $this->createClassConstFetch($className, 'class');
}
/**
* Creates "['item', $variable]"
*
* @param mixed[] $items
*/
public function createArray(array $items) : Array_
{
$arrayItems = [];
$defaultKey = 0;
foreach ($items as $key => $item) {
$customKey = $key !== $defaultKey ? $key : null;
$arrayItems[] = $this->createArrayItem($item, $customKey);
++$defaultKey;
}
return new Array_($arrayItems);
}
/**
* Creates "($args)"
*
* @param mixed[] $values
* @return Arg[]
*/
public function createArgs(array $values) : array
{
return $this->builderFactory->args($values);
}
/**
* Creates $this->property = $property;
*/
public function createPropertyAssignment(string $propertyName) : Assign
{
$variable = new Variable($propertyName);
return $this->createPropertyAssignmentWithExpr($propertyName, $variable);
}
/**
* @api
*/
public function createPropertyAssignmentWithExpr(string $propertyName, Expr $expr) : Assign
{
$propertyFetch = $this->createPropertyFetch(self::THIS, $propertyName);
return new Assign($propertyFetch, $expr);
}
/**
* @param mixed $argument
*/
public function createArg($argument) : Arg
{
return new Arg(BuilderHelpers::normalizeValue($argument));
}
public function createPublicMethod(string $name) : ClassMethod
{
$method = new Method($name);
$method->makePublic();
return $method->getNode();
}
public function createParamFromNameAndType(string $name, ?Type $type) : Param
{
$param = new ParamBuilder($name);
if ($type instanceof Type) {
$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::PARAM);
if ($typeNode instanceof Node) {
$param->setType($typeNode);
}
}
return $param->getNode();
}
public function createPrivatePropertyFromNameAndType(string $name, ?Type $type) : Property
{
$propertyBuilder = new PropertyBuilder($name);
$propertyBuilder->makePrivate();
$property = $propertyBuilder->getNode();
$this->propertyTypeDecorator->decorate($property, $type);
return $property;
}
/**
* @api symfony
* @param mixed[] $arguments
*/
public function createLocalMethodCall(string $method, array $arguments = []) : MethodCall
{
$variable = new Variable('this');
return $this->createMethodCall($variable, $method, $arguments);
}
/**
* @param mixed[] $arguments
* @param \PhpParser\Node\Expr|string $exprOrVariableName
*/
public function createMethodCall($exprOrVariableName, string $method, array $arguments = []) : MethodCall
{
$callerExpr = $this->createMethodCaller($exprOrVariableName);
return $this->builderFactory->methodCall($callerExpr, $method, $arguments);
}
/**
* @param string|\PhpParser\Node\Expr $variableNameOrExpr
*/
public function createPropertyFetch($variableNameOrExpr, string $property) : PropertyFetch
{
$fetcherExpr = \is_string($variableNameOrExpr) ? new Variable($variableNameOrExpr) : $variableNameOrExpr;
return $this->builderFactory->propertyFetch($fetcherExpr, $property);
}
/**
* @api doctrine
*/
public function createPrivateProperty(string $name) : Property
{
$propertyBuilder = new PropertyBuilder($name);
$propertyBuilder->makePrivate();
$property = $propertyBuilder->getNode();
$this->phpDocInfoFactory->createFromNode($property);
return $property;
}
/**
* @param Expr[] $exprs
*/
public function createConcat(array $exprs) : ?Concat
{
if (\count($exprs) < 2) {
return null;
}
$previousConcat = \array_shift($exprs);
foreach ($exprs as $expr) {
$previousConcat = new Concat($previousConcat, $expr);
}
if (!$previousConcat instanceof Concat) {
throw new ShouldNotHappenException();
}
return $previousConcat;
}
/**
* @param string|ObjectReference::* $class
* @param Node[] $args
*/
public function createStaticCall(string $class, string $method, array $args = []) : StaticCall
{
$name = $this->createName($class);
$args = $this->createArgs($args);
return new StaticCall($name, $method, $args);
}
/**
* @param mixed[] $arguments
*/
public function createFuncCall(string $name, array $arguments = []) : FuncCall
{
$arguments = $this->createArgs($arguments);
return new FuncCall(new Name($name), $arguments);
}
public function createSelfFetchConstant(string $constantName) : ClassConstFetch
{
$name = new Name(ObjectReference::SELF);
return new ClassConstFetch($name, $constantName);
}
public function createNull() : ConstFetch
{
return new ConstFetch(new Name('null'));
}
public function createPromotedPropertyParam(PropertyMetadata $propertyMetadata) : Param
{
$paramBuilder = new ParamBuilder($propertyMetadata->getName());
$propertyType = $propertyMetadata->getType();
if ($propertyType instanceof Type) {
$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PROPERTY);
if ($typeNode instanceof Node) {
$paramBuilder->setType($typeNode);
}
}
$param = $paramBuilder->getNode();
$propertyFlags = $propertyMetadata->getFlags();
$param->flags = $propertyFlags !== 0 ? $propertyFlags : Class_::MODIFIER_PRIVATE;
return $param;
}
public function createFalse() : ConstFetch
{
return new ConstFetch(new Name('false'));
}
public function createTrue() : ConstFetch
{
return new ConstFetch(new Name('true'));
}
/**
* @api phpunit
* @param string|ObjectReference::* $constantName
*/
public function createClassConstFetchFromName(Name $className, string $constantName) : ClassConstFetch
{
return $this->builderFactory->classConstFetch($className, $constantName);
}
/**
* @param array