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

Refactor code to avoid trait usage #363

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ function($class) {
'theseer\\phpdox\\projectconfig' => '/config/ProjectConfig.php',
'theseer\\phpdox\\shellprogresslogger' => '/logger/ShellProgressLogger.php',
'theseer\\phpdox\\silentprogresslogger' => '/logger/SilentProgressLogger.php',
'theseer\\phpdox\\typeawareinterface' => '/shared/TypeAwareInterface.php',
'theseer\\phpdox\\typeawaretrait' => '/shared/TypeAwareTrait.php',
'theseer\\phpdox\\typeinfo' => '/shared/TypeInfo.php',
'theseer\\phpdox\\version' => '/shared/Version.php'
);
}
Expand Down
24 changes: 15 additions & 9 deletions src/collector/backend/parser/UnitCollectingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
use TheSeer\phpDox\Collector\InlineComment;
use TheSeer\phpDox\Collector\MethodObject;
use TheSeer\phpDox\DocBlock\Parser as DocBlockParser;
use TheSeer\phpDox\TypeAwareInterface;
use TheSeer\phpDox\TypeAwareTrait;
use TheSeer\phpDox\TypeInfo;

class UnitCollectingVisitor extends NodeVisitorAbstract implements TypeAwareInterface {
use TypeAwareTrait;
class UnitCollectingVisitor extends NodeVisitorAbstract {

/**
* @var \TheSeer\phpDox\DocBlock\Parser
Expand All @@ -52,6 +50,11 @@ class UnitCollectingVisitor extends NodeVisitorAbstract implements TypeAwareInte
*/
private $unit;

/**
* @var TypeInfo
*/
private $typeInfo;

private $modifier = [
NodeType\Class_::MODIFIER_PUBLIC => 'public',
NodeType\Class_::MODIFIER_PROTECTED => 'protected',
Expand All @@ -61,6 +64,7 @@ class UnitCollectingVisitor extends NodeVisitorAbstract implements TypeAwareInte
public function __construct(DocBlockParser $parser, ParseResult $result) {
$this->docBlockParser = $parser;
$this->result = $result;
$this->typeInfo = new TypeInfo();
}

/**
Expand Down Expand Up @@ -266,25 +270,27 @@ private function processMethodReturnType(MethodObject $method, $returnType): voi
return;
}

if ($this->isBuiltInType((string)$returnType, self::TYPE_RETURN)) {
$returnTypeObject = $method->setReturnType($returnType);
$stringReturnType = $returnType instanceof NullableType ? (string)$returnType->type:(string)$returnType;

if ($this->typeInfo->isBuiltInType($stringReturnType, TypeInfo::TYPE_RETURN)) {
$returnTypeObject = $method->setReturnType($stringReturnType);
$returnTypeObject->setNullable(false);

return;
}

if ($returnType instanceof \PhpParser\Node\Name\FullyQualified) {
$returnTypeObject = $method->setReturnType($returnType->toString());
$returnTypeObject = $method->setReturnType($stringReturnType);
$returnTypeObject->setNullable(false);

return;
}

if ($returnType instanceof NullableType) {
if ((string)$returnType->type === 'self') {
if ($stringReturnType === 'self') {
$returnTypeObject = $method->setReturnType($this->unit->getName());
} else {
$returnTypeObject = $method->setReturnType($returnType->type);
$returnTypeObject = $method->setReturnType($stringReturnType);
}
$returnTypeObject->setNullable(true);

Expand Down
17 changes: 10 additions & 7 deletions src/collector/project/AbstractVariableObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
namespace TheSeer\phpDox\Collector;

use TheSeer\fDOM\fDOMElement;
use TheSeer\phpDox\TypeAwareInterface;
use TheSeer\phpDox\TypeAwareTrait;
use TheSeer\phpDox\TypeInfo;

/**
* Class AbstractVariableObject
*/
abstract class AbstractVariableObject implements TypeAwareInterface {
use TypeAwareTrait;

abstract class AbstractVariableObject {
public const XMLNS = 'http://xml.phpdox.net/src';

/**
Expand All @@ -23,8 +20,14 @@ abstract class AbstractVariableObject implements TypeAwareInterface {
*/
private $types = [];

/**
* @var TypeInfo
*/
private $typeInfo;

public function __construct(fDOMElement $ctx) {
$this->ctx = $ctx;
$this->ctx = $ctx;
$this->typeInfo = new TypeInfo();
}

public function export(): fDOMElement {
Expand Down Expand Up @@ -65,7 +68,7 @@ public function setConstant($const): void {
}

public function isInternalType($type) {
return $this->isBuiltInType((string)$type) || \in_array(\mb_strtolower((string)$type), $this->types);
return $this->typeInfo->isBuiltInType((string)$type) || \in_array(\mb_strtolower((string)$type), $this->types);
}

/**
Expand Down
21 changes: 15 additions & 6 deletions src/docblock/elements/VarElement.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
<?php declare(strict_types = 1);
namespace TheSeer\phpDox\DocBlock;

use TheSeer\phpDox\TypeAwareInterface;
use TheSeer\phpDox\TypeAwareTrait;
use TheSeer\phpDox\TypeInfo;

class VarElement extends GenericElement implements TypeAwareInterface {
use TypeAwareTrait;
class VarElement extends GenericElement {

public const XMLNS = 'http://xml.phpdox.net/src';

/**
* @var TypeInfo
*/
private $typeInfo;

public function __construct(Factory $factory, $name) {
parent::__construct($factory, $name);
$this->typeInfo = new TypeInfo();
}


public function asDom(\TheSeer\fDOM\fDOMDocument $ctx) {
$node = parent::asDom($ctx);
$type = $node->getAttribute('type');
Expand All @@ -31,7 +40,7 @@ public function asDom(\TheSeer\fDOM\fDOMDocument $ctx) {
$node->setAttribute('of', $type);
}

if (!$this->isBuiltInType($type, self::TYPE_PHPDOC|self::TYPE_PHPDOX)) {
if (!$this->typeInfo->isBuiltInType($type, TypeInfo::TYPE_PHPDOC|TypeInfo::TYPE_PHPDOX)) {
if (!$node->hasAttribute('of')) {
$node->setAttribute('type', 'object');
} else {
Expand Down Expand Up @@ -60,7 +69,7 @@ protected function typeResolver(\TheSeer\fDOM\fDOMElement $node, string $type) {
$nodeType->setAttribute('full', $type);
$nodeType->setAttribute('array', $isArray?'true':'false');

if ($this->isBuiltInType($type, self::TYPE_PHPDOC|self::TYPE_PHPDOX)) {
if ($this->typeInfo->isBuiltInType($type, TypeInfo::TYPE_PHPDOC|TypeInfo::TYPE_PHPDOX)) {
$nodeType->setAttribute('name', $type);
$nodeType->setAttribute('namespace', '');
return;
Expand Down
18 changes: 11 additions & 7 deletions src/docblock/parser/GenericParser.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php declare(strict_types = 1);
namespace TheSeer\phpDox\DocBlock;

use TheSeer\phpDox\TypeAwareInterface;
use TheSeer\phpDox\TypeAwareTrait;
use TheSeer\phpDox\TypeInfo;

class GenericParser implements TypeAwareInterface {
use TypeAwareTrait;
class GenericParser {

protected $factory;

Expand All @@ -15,9 +13,15 @@ class GenericParser implements TypeAwareInterface {

protected $payload;

/**
* @var TypeInfo
*/
protected $typeInfo;

public function __construct(Factory $factory, $name) {
$this->factory = $factory;
$this->name = $name;
$this->factory = $factory;
$this->name = $name;
$this->typeInfo = new TypeInfo();
}

public function setAliasMap(array $map): void {
Expand Down Expand Up @@ -72,7 +76,7 @@ protected function lookupOneType($type) {
}

// Do not mess with scalar and fixed types
if ($this->isBuiltInType($type)) {
if ($this->typeInfo->isBuiltInType($type)) {
return $type;
}

Expand Down
14 changes: 0 additions & 14 deletions src/shared/TypeAwareInterface.php

This file was deleted.

20 changes: 13 additions & 7 deletions src/shared/TypeAwareTrait.php → src/shared/TypeInfo.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<?php
namespace TheSeer\phpDox;

trait TypeAwareTrait
class TypeInfo
{
public function getBuiltInTypes(int $context = TypeAwareInterface::TYPE_ALL): array {
public const TYPE_RETURN = 1;
public const TYPE_PHPDOC = 2;
public const TYPE_BUILTIN = 4;
public const TYPE_PHPDOX = 8;
public const TYPE_ALL = self::TYPE_RETURN + self::TYPE_PHPDOC + self::TYPE_BUILTIN + self::TYPE_PHPDOX;

public function getBuiltInTypes(int $context = self::TYPE_ALL): array {
/*
* From:
* http://docs.phpdoc.org/guides/types.html#primitives
Expand Down Expand Up @@ -31,23 +37,23 @@ public function getBuiltInTypes(int $context = TypeAwareInterface::TYPE_ALL): ar
$type = [];

switch (true) {
case ($context & TypeAwareInterface::TYPE_RETURN) !== 0:
case ($context & self::TYPE_RETURN) !== 0:
$type = array_merge($type, $returnType);
// no-break
case ($context & TypeAwareInterface::TYPE_PHPDOC) !== 0:
case ($context & self::TYPE_PHPDOC) !== 0:
$type = array_merge($type, $docblockType);
// no-break
case ($context & TypeAwareInterface::TYPE_BUILTIN) !== 0:
case ($context & self::TYPE_BUILTIN) !== 0:
$type = array_merge($type, $variableType);
// no-break
case ($context & TypeAwareInterface::TYPE_PHPDOX) !== 0:
case ($context & self::TYPE_PHPDOX) !== 0:
$type = array_merge($type, $phpdoxType);
// no-break
}

return array_unique($type);
}
public function isBuiltInType(string $type, int $context = TypeAwareInterface::TYPE_ALL): bool {
public function isBuiltInType(string $type, int $context = self::TYPE_ALL): bool {
return \in_array(\mb_strtolower($type), $this->getBuiltInTypes($context), true);
}
}