Skip to content

Commit

Permalink
feat: Added php documentator methods in type factory
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykbaszak committed Aug 1, 2024
1 parent bc19ab5 commit bd21ec9
Showing 1 changed file with 88 additions and 14 deletions.
102 changes: 88 additions & 14 deletions src/Reflection/Domain/Factories/TypeReflectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,108 @@

namespace PBaszak\UltraMapper\Reflection\Domain\Factories;

use PBaszak\UltraMapper\Reflection\Domain\Entities\Type\NamedTypeReflection;
use PBaszak\UltraMapper\Reflection\Domain\Entities\Type\TypeReflection;
use phpDocumentor\Reflection\DocBlock\Tags\Param;
use phpDocumentor\Reflection\DocBlock\Tags\Return_;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\Type as PhpDocumentorReflectionType;

class TypeReflectionFactory
{
public function createForParameter(\ReflectionParameter $reflectionParameter): TypeReflection
{
// $typeReflection = TypeReflection::create($reflectionParameter->getType());
// $typeReflection->raise(new ReflectionCreated($typeReflection->id()));

// return $typeReflection;
throw new \Exception('Not implemented');
return $this->create(
$reflectionParameter->getType(),
$this->getDocBlockReflectionTypeFromParamTag($reflectionParameter)
);
}

public function createForProperty(\ReflectionProperty $reflectionProperty): TypeReflection
{
// $typeReflection = TypeReflection::create($reflectionProperty->getType());
// $typeReflection->raise(new ReflectionCreated($typeReflection->id()));

// return $typeReflection;
throw new \Exception('Not implemented');
return $this->create(
$reflectionProperty->getType(),
$this->getDocBlockReflectionTypeFromVarTag($reflectionProperty)
);
}

public function createForMethod(\ReflectionMethod $reflectionMethod): TypeReflection
{
// $typeReflection = TypeReflection::create($reflectionMethod->getReturnType());
// $typeReflection->raise(new ReflectionCreated($typeReflection->id()));
return $this->create(
$reflectionMethod->getReturnType(),
$this->getDocBlockReflectionTypeFromReturnTag($reflectionMethod)
);
}

public function create(?\ReflectionType $ref, ?PhpDocumentorReflectionType $docCommentRef): TypeReflection
{
return NamedTypeReflection::create('mixed', NamedTypeReflection::IS_BUILD_IN);
}

private function getDocBlockReflectionTypeFromVarTag(\ReflectionProperty $ref): ?PhpDocumentorReflectionType
{
$docBlock = $ref->getDocComment();
if (false === $docBlock) {
return null;
}

$factory = DocBlockFactory::createInstance();
$docBlock = $factory->create($docBlock);

/** @var Var_[] $tags */
$tags = $docBlock->getTagsByName('var');

if (0 === count($tags)) {
return null;
}

return $tags[0]->getType();
}

private function getDocBlockReflectionTypeFromReturnTag(\ReflectionMethod $ref): ?PhpDocumentorReflectionType
{
$docBlock = $ref->getDocComment();
if (false === $docBlock) {
return null;
}

$factory = DocBlockFactory::createInstance();
$docBlock = $factory->create($docBlock);

/** @var Return_[] $tags */
$tags = $docBlock->getTagsByName('return');

if (0 === count($tags)) {
return null;
}

return $tags[0]->getType();
}

private function getDocBlockReflectionTypeFromParamTag(\ReflectionParameter $ref): ?PhpDocumentorReflectionType
{
$docBlock = $ref->getDeclaringFunction()->getDocComment();
if (false === $docBlock) {
return null;
}

$factory = DocBlockFactory::createInstance();
$docBlock = $factory->create($docBlock);

/** @var Param[] $tags */
$tags = $docBlock->getTagsByName('param');

if (0 === count($tags)) {
return null;
}

foreach ($tags as $tag) {
if ($tag->getVariableName() === $ref->getName()) {
return $tag->getType();
}
}

// return $typeReflection;
throw new \Exception('Not implemented');
return null;
}
}

0 comments on commit bd21ec9

Please sign in to comment.