Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add/update type information for source files #68

Merged
merged 1 commit into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/DeclareStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class DeclareStatement
self::ENCODING => 'string',
];

/** @var string */
protected $directive;
protected string $directive;

/** @var int|string */
protected $value;
Expand Down
17 changes: 9 additions & 8 deletions src/Generator/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ abstract class AbstractGenerator implements GeneratorInterface
*/
public const LINE_FEED = "\n";

/** @var bool */
protected $isSourceDirty = true;
protected bool $isSourceDirty = true;

/** @var int|string 4 spaces by default */
protected $indentation = ' ';
/** @var string 4 spaces by default */
protected string $indentation = ' ';

/** @var string */
protected $sourceContent;
/**
* TODO: Type should be changed to "string" in the next major version. Nullable for BC
*/
protected ?string $sourceContent = null;

/**
* @param array $options
Expand Down Expand Up @@ -80,7 +81,7 @@ public function getIndentation()
}

/**
* @param string $sourceContent
* @param ?string $sourceContent
* @return AbstractGenerator
*/
public function setSourceContent($sourceContent)
Expand All @@ -90,7 +91,7 @@ public function setSourceContent($sourceContent)
}

/**
* @return string
* @return ?string
*/
public function getSourceContent()
{
Expand Down
11 changes: 4 additions & 7 deletions src/Generator/AbstractMemberGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@ abstract class AbstractMemberGenerator extends AbstractGenerator
public const VISIBILITY_PROTECTED = 'protected';
public const VISIBILITY_PRIVATE = 'private';

/** @var DocBlockGenerator|null */
protected $docBlock;
protected ?DocBlockGenerator $docBlock = null;

/** @var string */
protected $name;
protected string $name = '';

/** @var int */
protected $flags = self::FLAG_PUBLIC;
protected int $flags = self::FLAG_PUBLIC;

/**
* @param int|array $flags
* @param int|int[] $flags
* @return AbstractMemberGenerator
*/
public function setFlags($flags)
Expand Down
3 changes: 1 addition & 2 deletions src/Generator/BodyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

class BodyGenerator extends AbstractGenerator
{
/** @var string */
protected $content;
protected string $content = '';

/**
* @param string $content
Expand Down
116 changes: 58 additions & 58 deletions src/Generator/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,38 @@ class ClassGenerator extends AbstractGenerator implements TraitUsageInterface
public const FLAG_ABSTRACT = 0x01;
public const FLAG_FINAL = 0x02;

/** @var FileGenerator */
protected $containingFileGenerator;
protected ?FileGenerator $containingFileGenerator = null;

/** @var string */
protected $namespaceName;
protected ?string $namespaceName = null;

/** @var DocBlockGenerator */
protected $docBlock;
protected ?DocBlockGenerator $docBlock = null;

/** @var string */
protected $name;
protected string $name = '';

/** @var bool */
protected $flags = 0x00;
protected int $flags = 0x00;

/** @var string */
protected $extendedClass;
/** @psalm-var ?class-string */
protected ?string $extendedClass = null;

/**
* @var string[] Array of string names
* Array of implemented interface names
*
* @var string[]
* @psalm-var array<class-string>
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
*/
protected $implementedInterfaces = [];
protected array $implementedInterfaces = [];

/** @var PropertyGenerator[] */
protected $properties = [];
protected array $properties = [];

/** @var PropertyGenerator[] */
protected $constants = [];
protected array $constants = [];

/** @var MethodGenerator[] */
protected $methods = [];
protected array $methods = [];

/** @var TraitUsageGenerator Object to encapsulate trait usage logic */
protected $traitUsageGenerator;
protected TraitUsageGenerator $traitUsageGenerator;

/**
* Build a Code Generation Php Object from a Class Reflection
Expand Down Expand Up @@ -109,7 +106,6 @@ public static function fromReflection(ClassReflection $classReflection)

$interfaceNames = [];
foreach ($interfaces as $interface) {
/** @var ClassReflection $interface */
$interfaceNames[] = $interface->getName();
}

Expand Down Expand Up @@ -139,7 +135,11 @@ public static function fromReflection(ClassReflection $classReflection)
$methods = [];

foreach ($classReflection->getMethods() as $reflectionMethod) {
$className = $cg->getNamespaceName() ? $cg->getNamespaceName() . '\\' . $cg->getName() : $cg->getName();
$className = $cg->getName();
$namespaceName = $cg->getNamespaceName();
if ($namespaceName !== null) {
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
$className = $namespaceName . '\\' . $className;
}

if ($reflectionMethod->getDeclaringClass()->getName() == $className) {
$methods[] = MethodGenerator::fromReflection($reflectionMethod);
Expand Down Expand Up @@ -211,23 +211,24 @@ public static function fromArray(array $array)
}

/**
* @param string $name
* @param string $namespaceName
* @param array|string $flags
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
* @param string $extends
* @param array $interfaces
* @param array $properties
* @param array $methods
* @param DocBlockGenerator $docBlock
* @param string $name
* @param string $namespaceName
* @param int|int[]|null $flags
* @param class-string|null $extends
* @param string[] $interfaces
* @psalm-param array<class-string> $interfaces
* @param PropertyGenerator[]|string[]|array[] $properties
* @param MethodGenerator[]|string[]|array[] $methods
* @param DocBlockGenerator $docBlock
*/
public function __construct(
$name = null,
$namespaceName = null,
$flags = null,
$extends = null,
$interfaces = [],
$properties = [],
$methods = [],
array $interfaces = [],
array $properties = [],
array $methods = [],
$docBlock = null
) {
$this->traitUsageGenerator = new TraitUsageGenerator($this);
Expand All @@ -247,7 +248,7 @@ public function __construct(
if ($extends !== null) {
$this->setExtendedClass($extends);
}
if (is_array($interfaces)) {
if ($interfaces !== []) {
$this->setImplementedInterfaces($interfaces);
}
if ($methods !== []) {
Expand Down Expand Up @@ -283,7 +284,7 @@ public function getName()
}

/**
* @param string $namespaceName
* @param ?string $namespaceName
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
* @return self
*/
public function setNamespaceName($namespaceName)
Expand All @@ -293,7 +294,7 @@ public function setNamespaceName($namespaceName)
}

/**
* @return string
* @return ?string
*/
public function getNamespaceName()
{
Expand All @@ -310,7 +311,7 @@ public function setContainingFileGenerator(FileGenerator $fileGenerator)
}

/**
* @return FileGenerator
* @return ?FileGenerator
*/
public function getContainingFileGenerator()
{
Expand All @@ -327,15 +328,15 @@ public function setDocBlock(DocBlockGenerator $docBlock)
}

/**
* @return DocBlockGenerator
* @return ?DocBlockGenerator
*/
public function getDocBlock()
{
return $this->docBlock;
}

/**
* @param array|string $flags
* @param int[]|int $flags
* @return self
*/
public function setFlags($flags)
Expand All @@ -354,7 +355,7 @@ public function setFlags($flags)
}

/**
* @param string $flag
* @param int $flag
* @return self
*/
public function addFlag($flag)
Expand All @@ -364,7 +365,7 @@ public function addFlag($flag)
}

/**
* @param string $flag
* @param int $flag
* @return self
*/
public function removeFlag($flag)
Expand Down Expand Up @@ -404,11 +405,12 @@ public function setFinal($isFinal)
*/
public function isFinal()
{
return $this->flags & self::FLAG_FINAL;
return (bool) ($this->flags & self::FLAG_FINAL);
}

/**
* @param string $extendedClass
* @param ?string $extendedClass
* @psalm-param ?class-string $extendedClass
* @return self
*/
public function setExtendedClass($extendedClass)
Expand All @@ -418,7 +420,8 @@ public function setExtendedClass($extendedClass)
}

/**
* @return string
* @return ?string
* @psalm-return ?class-string
*/
public function getExtendedClass()
{
Expand Down Expand Up @@ -459,7 +462,7 @@ public function setImplementedInterfaces(array $implementedInterfaces)
}

/**
* @return string
* @return string[]
* @psalm-return array<class-string>
*/
public function getImplementedInterfaces()
Expand All @@ -469,6 +472,7 @@ public function getImplementedInterfaces()

/**
* @param string $implementedInterface
* @psalm-param class-string $implementedInterface
* @return bool
*/
public function hasImplementedInterface($implementedInterface)
Expand All @@ -491,8 +495,8 @@ public function removeImplementedInterface($implementedInterface)
$interfaceType = TypeGenerator::fromTypeString($implementedInterface);

$this->implementedInterfaces = array_filter(
array_map([TypeGenerator::class, 'fromTypeString'], $this->implementedInterfaces),
static fn (TypeGenerator $interface): bool => ! $interfaceType->equals($interface)
$this->implementedInterfaces,
static fn (string $interface): bool => ! TypeGenerator::fromTypeString($interface)->equals($interfaceType)
);

return $this;
Expand Down Expand Up @@ -612,20 +616,18 @@ public function addConstants(array $constants)
}

/**
* @param array $properties
* @param PropertyGenerator[]|string[]|array[] $properties
* @return self
*/
public function addProperties(array $properties)
{
foreach ($properties as $property) {
if ($property instanceof PropertyGenerator) {
$this->addPropertyFromGenerator($property);
} elseif (is_string($property)) {
$this->addProperty($property);
} else {
if (is_string($property)) {
$this->addProperty($property);
} elseif (is_array($property)) {
$this->addProperty(...array_values($property));
}
$this->addProperty(...array_values($property));
}
}

Expand Down Expand Up @@ -792,20 +794,18 @@ public function hasProperty($propertyName)
}

/**
* @param array $methods
* @param MethodGenerator[]|string[]|array[] $methods
* @return self
*/
public function addMethods(array $methods)
{
foreach ($methods as $method) {
if ($method instanceof MethodGenerator) {
$this->addMethodFromGenerator($method);
} elseif (is_string($method)) {
$this->addMethod($method);
} else {
if (is_string($method)) {
$this->addMethod($method);
} elseif (is_array($method)) {
$this->addMethod(...array_values($method));
}
$this->addMethod(...array_values($method));
}
}

Expand All @@ -816,7 +816,7 @@ public function addMethods(array $methods)
* Add Method from scalars
*
* @param string $name
* @param array $parameters
* @param ParameterGenerator[]|array[]|string[] $parameters
* @param int $flags
* @param string $body
* @param string $docBlock
Expand Down
Loading