Skip to content

Commit

Permalink
Add support for PHP 7 return types to collector
Browse files Browse the repository at this point in the history
  • Loading branch information
theseer committed Jun 11, 2017
1 parent 0dfe864 commit 6b52a10
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function($class) {
'theseer\\phpdox\\collector\\parameterobject' => '/collector/project/ParameterObject.php',
'theseer\\phpdox\\collector\\project' => '/collector/project/Project.php',
'theseer\\phpdox\\collector\\projectexception' => '/collector/project/ProjectException.php',
'theseer\\phpdox\\collector\\returntypeobject' => '/collector/project/ReturnTypeObject.php',
'theseer\\phpdox\\collector\\sourcecollection' => '/collector/project/SourceCollection.php',
'theseer\\phpdox\\collector\\sourcecollectionexception' => '/collector/project/SourceCollectionException.php',
'theseer\\phpdox\\collector\\sourcefile' => '/collector/project/SourceFile.php',
Expand Down
28 changes: 28 additions & 0 deletions src/collector/backend/parser/UnitCollectingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*/
namespace TheSeer\phpDox\Collector\Backend {

use TheSeer\DirectoryScanner\Exception;
use TheSeer\phpDox\Collector\AbstractUnitObject;
use TheSeer\phpDox\Collector\AbstractVariableObject;
use TheSeer\phpDox\Collector\InlineComment;
Expand Down Expand Up @@ -227,6 +228,8 @@ private function processMethod(NodeType\ClassMethod $node) {
$method->setFinal($node->isFinal());
$method->setStatic($node->isStatic());

$this->processMethodReturnType($method, $node->getReturnType());

$visibility = 'public';
if ($node->isPrivate()) {
$visibility = 'private';
Expand All @@ -248,6 +251,31 @@ private function processMethod(NodeType\ClassMethod $node) {
}
}

private function processMethodReturnType(MethodObject $method, $returnType) {
if ($returnType === null) {
return;
}

if (in_array($returnType, ['void','float','int','string','bool','callable','array'])) {
$returnTypeObject = $method->setReturnType($returnType);
$returnTypeObject->setNullable(false);
return;
}

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

if ($returnType instanceof \PhpParser\Node\NullableType) {
$returnTypeObject = $method->setReturnType($returnType->type);
$returnTypeObject->setNullable(true);
return;
}
throw new ParseErrorException("Unexpected return type definition", ParseErrorException::UnexpectedExpr);
}

private function processInlineComments(MethodObject $method, array $stmts) {
foreach($stmts as $stmt) {
if ($stmt->hasAttribute('comments')) {
Expand Down
5 changes: 4 additions & 1 deletion src/collector/project/AbstractVariableObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class AbstractVariableObject {
/**
* @var array
*/
private $types = array('{unknown}', 'object', 'array','integer','float','string','boolean','resource');
private $types = array('{unknown}', 'object', 'array', 'int', 'integer','float','string','bool','boolean','resource','callable');

/**
* @param fDOMElement $ctx
Expand Down Expand Up @@ -135,5 +135,8 @@ public function getType() {
return $this->ctx->getAttribute('type');
}

protected function addInternalType($type) {
$this->types[] = $type;
}
}
}
10 changes: 10 additions & 0 deletions src/collector/project/MethodObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ public function inhertDocBlock(MethodObject $method) {

}

/**
* @param string $name
*
* @return ReturnTypeObject
*/
public function setReturnType($name) {
$returnType = new ReturnTypeObject($this->ctx->appendElementNS(self::XMLNS, 'return'));
$returnType->setType($name);
return $returnType;
}

/**
* @param string $name
Expand Down
54 changes: 54 additions & 0 deletions src/collector/project/ReturnTypeObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright (c) 2010-2017 Arne Blankerts <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Arne Blankerts nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT * NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER ORCONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package phpDox
* @author Arne Blankerts <[email protected]>
* @copyright Arne Blankerts <[email protected]>, All rights reserved.
* @license BSD License
*/
namespace TheSeer\phpDox\Collector {

use TheSeer\fDOM\fDOMElement;

class ReturnTypeObject extends AbstractVariableObject {

public function __construct(fDOMElement $ctx) {
parent::__construct($ctx);
$this->addInternalType('void');
}

public function setNullable($isNullable) {
$this->ctx->setAttribute('nullable', $isNullable ? 'true' : 'false');
}

}

}
34 changes: 34 additions & 0 deletions tests/data/nullable/src/nullable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace some\test;

class NullableReturnType {

public function floatReturn(): ?float {
return 1.0;
}

public function intReturn(): ?int {
return 1;
}

public function stringReturn(): ?string {
return 'abc';
}

public function callableReturn(): ?Callable {
return function () {};
}

public function boolReturn(): ?bool {
return false;
}

public function arrayReturn(): ?array {
return [];
}

public function objectReturn(): ?ReturnType {
return new ReturnType();
}

}
15 changes: 15 additions & 0 deletions tests/data/nullable/test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpdox xmlns="http://xml.phpdox.net/config" silent="false">

<project name="phpDox-nullable" source="${basedir}/src" workdir="${basedir}/xml">

<collector publiconly="false" backend="parser" />

<generator output="${basedir}/docs">
<build engine="html" enabled="true" output="html" />
<build engine="xml" enabled="true" output="xml" />
</generator>

</project>

</phpdox>
40 changes: 40 additions & 0 deletions tests/data/returntype/src/returntype.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace some\test;

class ReturnType {

public function unspecifiedReturn() {
}

public function voidReturn(): void {
}

public function floatReturn(): float {
return 1.0;
}

public function intReturn(): int {
return 1;
}

public function stringReturn(): string {
return 'abc';
}

public function callableReturn(): Callable {
return function () {};
}

public function boolReturn(): bool {
return false;
}

public function arrayReturn(): array {
return [];
}

public function always(): ReturnType {
return new ReturnType();
}

}
15 changes: 15 additions & 0 deletions tests/data/returntype/test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpdox xmlns="http://xml.phpdox.net/config" silent="false">

<project name="phpDox-returntype" source="${basedir}/src" workdir="${basedir}/xml">

<collector publiconly="false" backend="parser" />

<generator output="${basedir}/docs">
<build engine="html" enabled="true" output="html" />
<build engine="xml" enabled="true" output="xml" />
</generator>

</project>

</phpdox>

0 comments on commit 6b52a10

Please sign in to comment.