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

Fix typehint for callable #358

Merged
merged 1 commit into from
May 10, 2019
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
2 changes: 2 additions & 0 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ 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\\version' => '/shared/Version.php'
);
}
Expand Down
10 changes: 7 additions & 3 deletions src/collector/backend/parser/UnitCollectingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
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;

class UnitCollectingVisitor extends NodeVisitorAbstract implements TypeAwareInterface {
use TypeAwareTrait;

class UnitCollectingVisitor extends NodeVisitorAbstract {
/**
* @var \TheSeer\phpDox\DocBlock\Parser
*/
Expand Down Expand Up @@ -262,7 +266,7 @@ private function processMethodReturnType(MethodObject $method, $returnType): voi
return;
}

if (\in_array($returnType, ['void', 'float', 'int', 'string', 'bool', 'callable', 'array', 'object'])) {
if ($this->isBuiltInType((string)$returnType, self::TYPE_RETURN)) {
$returnTypeObject = $method->setReturnType($returnType);
$returnTypeObject->setNullable(false);

Expand Down Expand Up @@ -495,7 +499,7 @@ private function resolveExpressionValue(Expr $expr) {

return [
'type' => 'expression',
'value' => \substr($code, 0, -1)
'value' => $code
];
}

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

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

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

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

/**
Expand All @@ -17,7 +21,7 @@ abstract class AbstractVariableObject {
/**
* @var array
*/
private $types = ['{unknown}', 'object', 'array', 'int', 'integer', 'float', 'string', 'bool', 'boolean', 'resource', 'callable'];
private $types = [];

public function __construct(fDOMElement $ctx) {
$this->ctx = $ctx;
Expand Down Expand Up @@ -61,7 +65,7 @@ public function setConstant($const): void {
}

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

/**
Expand Down
17 changes: 7 additions & 10 deletions src/docblock/elements/VarElement.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<?php declare(strict_types = 1);
namespace TheSeer\phpDox\DocBlock;

class VarElement extends GenericElement {
public const XMLNS = 'http://xml.phpdox.net/src';
use TheSeer\phpDox\TypeAwareInterface;
use TheSeer\phpDox\TypeAwareTrait;

class VarElement extends GenericElement implements TypeAwareInterface {
use TypeAwareTrait;

/**
* @var string[]
*/
private $types = [
'', 'null', 'mixed', '{unknown}', 'object', 'array', 'integer', 'int',
'float', 'string', 'boolean', 'resource'
];
public const XMLNS = 'http://xml.phpdox.net/src';

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

if (!\in_array($type, $this->types)) {
if (!$this->isBuiltInType($type, self::TYPE_PHPDOC|self::TYPE_PHPDOX)) {
if (!$node->hasAttribute('of')) {
$node->setAttribute('type', 'object');
} else {
Expand Down
14 changes: 7 additions & 7 deletions src/docblock/parser/GenericParser.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php declare(strict_types = 1);
namespace TheSeer\phpDox\DocBlock;

class GenericParser {
use TheSeer\phpDox\TypeAwareInterface;
use TheSeer\phpDox\TypeAwareTrait;

class GenericParser implements TypeAwareInterface {
use TypeAwareTrait;

protected $factory;

protected $aliasMap;
Expand All @@ -10,11 +15,6 @@ class GenericParser {

protected $payload;

private $types = [
'', 'null', 'mixed', '{unknown}', 'object', 'array', 'integer', 'int',
'float', 'string', 'boolean', 'resource'
];

public function __construct(Factory $factory, $name) {
$this->factory = $factory;
$this->name = $name;
Expand Down Expand Up @@ -52,7 +52,7 @@ protected function lookupType($type) {
}

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

Expand Down
14 changes: 14 additions & 0 deletions src/shared/TypeAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace TheSeer\phpDox;

interface TypeAwareInterface
{
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;
public function isBuiltInType(string $type, int $context = self::TYPE_ALL): bool;
}
53 changes: 53 additions & 0 deletions src/shared/TypeAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace TheSeer\phpDox;

trait TypeAwareTrait
{
public function getBuiltInTypes(int $context = TypeAwareInterface::TYPE_ALL): array {
/*
* From:
* http://docs.phpdoc.org/guides/types.html#primitives
* http://docs.phpdoc.org/guides/types.html#keywords
*/
$docblockType = ['string', 'int', 'integer', 'float', 'boolean', 'bool', 'array', 'resource', 'null', 'callable',
'mixed', 'void', 'object', 'true', 'false', 'self', 'static', '$this'];

$phpdoxType = ['', '{unknown}'];

/*
* From:
* https://www.php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
* + callable (https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.scalar-type-declarations)
*/
$variableType = ['object', 'array', 'int',
'float', 'string', 'callable', 'iterable', 'object', 'self', 'bool'];
/*
* From:
* https://www.php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
* + void (https://www.php.net/manual/en/migration71.new-features.php#migration71.new-features.void-functions)
*/
$returnType = $variableType + ['void'];

$type = [];

switch (true) {
case ($context & TypeAwareInterface::TYPE_RETURN) !== 0:
$type = array_merge($type, $returnType);
// no-break
case ($context & TypeAwareInterface::TYPE_PHPDOC) !== 0:
$type = array_merge($type, $docblockType);
// no-break
case ($context & TypeAwareInterface::TYPE_BUILTIN) !== 0:
$type = array_merge($type, $variableType);
// no-break
case ($context & TypeAwareInterface::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 {
return \in_array(\mb_strtolower($type), $this->getBuiltInTypes($context), true);
}
}
3 changes: 2 additions & 1 deletion tests/Integration/docblock/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testParse($src): void {

$factory = new Factory();
$parser = new Parser($factory);
$result = $parser->parse($block, []);
$result = $parser->parse($block, ['::unit' => '\\A']);

$this->assertInstanceOf(DocBlock::class, $result);

Expand All @@ -53,6 +53,7 @@ public function docblockSources() {
['heading'],
['multiat'],
['multiline_body'],
['param_all_type'],
['param_without_description'],
['param_without_varname'],
['param_without_varname_and_description'],
Expand Down
21 changes: 21 additions & 0 deletions tests/data/docbock/param_all_type
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param string $var1
* @param int $var2
* @param integer $var3
* @param bool $var4
* @param boolean $var5
* @param float $var6
* @param null $var7
* @param array $var8
* @param self $var9
* @param static $var10
* @param callable $var12
* @param resource $var13
* @param mixed $var14
* @param object $var15
* @param true $var16
* @param false $var17
* @param void $var18
* @param SomeType $var19
* @return $this
*/
28 changes: 28 additions & 0 deletions tests/data/docbock/param_all_type.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<docblock xmlns="http://xml.phpdox.net/src">
<description />
<param type="string" variable="$var1"/>
<param type="int" variable="$var2"/>
<param type="integer" variable="$var3"/>
<param type="bool" variable="$var4"/>
<param type="boolean" variable="$var5"/>
<param type="float" variable="$var6"/>
<param type="null" variable="$var7"/>
<param type="array" variable="$var8"/>
<param type="object" variable="$var9">
<type full="\A" name="A" namespace=""/>
</param>
<param type="object" variable="$var10">
<type full="\A" name="A" namespace=""/>
</param>
<param type="callable" variable="$var12"/>
<param type="resource" variable="$var13"/>
<param type="mixed" variable="$var14"/>
<param type="object" variable="$var15"/>
<param type="true" variable="$var16"/>
<param type="false" variable="$var17"/>
<param type="void" variable="$var18"/>
<param type="object" variable="$var19">
<type full="SomeType" name="SomeType" namespace=""/>
</param>
<return type="$this" />
</docblock>