Skip to content

Commit

Permalink
Add/update type information for source files
Browse files Browse the repository at this point in the history
Signed-off-by: Lisachenko Alexander <lisachenko.it@gmail.com>
  • Loading branch information
lisachenko committed Jan 10, 2021
1 parent 561e597 commit 5f17ebc
Show file tree
Hide file tree
Showing 20 changed files with 240 additions and 223 deletions.
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
10 changes: 4 additions & 6 deletions src/Generator/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ 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;
protected string $sourceContent = '';

/**
* @param array $options
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
106 changes: 49 additions & 57 deletions src/Generator/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,35 @@ 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;
protected ?string $extendedClass = null;

/**
* @var string[] Array of string names
* @psalm-var array<class-string>
* @psalm-var array<int, string>
*/
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 +103,6 @@ public static function fromReflection(ClassReflection $classReflection)

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

Expand Down Expand Up @@ -139,7 +132,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) {
$className = $namespaceName . '\\' . $className;
}

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

/**
* @param string $name
* @param string $namespaceName
* @param array|string $flags
* @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 array $interfaces
* @param PropertyGenerator[]|string[]|array[] $properties
* @param MethodGenerator[]|string[]|array[] $methods
* @param DocBlockGenerator $docBlock
*/
public function __construct(
$name = null,
Expand Down Expand Up @@ -283,7 +280,7 @@ public function getName()
}

/**
* @param string $namespaceName
* @param ?string $namespaceName
* @return self
*/
public function setNamespaceName($namespaceName)
Expand All @@ -293,7 +290,7 @@ public function setNamespaceName($namespaceName)
}

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

/**
* @return FileGenerator
* @return ?FileGenerator
*/
public function getContainingFileGenerator()
{
Expand All @@ -327,15 +324,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 +351,7 @@ public function setFlags($flags)
}

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

/**
* @param string $flag
* @param int $flag
* @return self
*/
public function removeFlag($flag)
Expand Down Expand Up @@ -404,11 +401,11 @@ 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
* @return self
*/
public function setExtendedClass($extendedClass)
Expand All @@ -418,7 +415,7 @@ public function setExtendedClass($extendedClass)
}

/**
* @return string
* @return ?string
*/
public function getExtendedClass()
{
Expand All @@ -444,7 +441,7 @@ public function removeExtentedClass()

/**
* @param string[] $implementedInterfaces
* @psalm-param array<class-string> $implementedInterfaces
* @psalm-param array<int, string> $implementedInterfaces
* @return self
*/
public function setImplementedInterfaces(array $implementedInterfaces)
Expand All @@ -459,8 +456,8 @@ public function setImplementedInterfaces(array $implementedInterfaces)
}

/**
* @return string
* @psalm-return array<class-string>
* @return string[]
* @psalm-return array<int, string>
*/
public function getImplementedInterfaces()
{
Expand All @@ -483,16 +480,15 @@ public function hasImplementedInterface($implementedInterface)

/**
* @param string $implementedInterface
* @psalm-param class-string $implementedInterface
* @return self
*/
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 +608,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 +786,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 +808,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

0 comments on commit 5f17ebc

Please sign in to comment.