This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/#41-var-tag-support-in-docblocks' into develop
Close #41
- Loading branch information
Showing
9 changed files
with
328 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Code\Generator\DocBlock\Tag; | ||
|
||
class VarTag extends AbstractTypeableTag implements TagInterface | ||
{ | ||
/** | ||
* @var string|null | ||
*/ | ||
private $variableName; | ||
|
||
/** | ||
* @param string|null $variableName | ||
* @param string|string[] $types | ||
* @param string|null $description | ||
*/ | ||
public function __construct(?string $variableName = null, $types = [], ?string $description = null) | ||
{ | ||
if (null !== $variableName) { | ||
$this->variableName = ltrim($variableName, '$'); | ||
} | ||
|
||
parent::__construct($types, $description); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getName() : string | ||
{ | ||
return 'var'; | ||
} | ||
|
||
/** | ||
* @internal this code is only public for compatibility with the | ||
* @see \Zend\Code\Generator\DocBlock\TagManager, which | ||
* uses setters | ||
*/ | ||
public function setVariableName(?string $variableName) : void | ||
{ | ||
if (null !== $variableName) { | ||
$this->variableName = ltrim($variableName, '$'); | ||
} | ||
} | ||
|
||
public function getVariableName() : ?string | ||
{ | ||
return $this->variableName; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function generate() : string | ||
{ | ||
return '@var' | ||
. ((!empty($this->types)) ? ' ' . $this->getTypesAsString() : '') | ||
. (null !== $this->variableName ? ' $' . $this->variableName : '') | ||
. ((!empty($this->description)) ? ' ' . $this->description : ''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Code\Reflection\DocBlock\Tag; | ||
|
||
class VarTag implements TagInterface, PhpDocTypedTagInterface | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private $types = []; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $variableName; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $description; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getName() : string | ||
{ | ||
return 'var'; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function initialize($tagDocblockLine) : void | ||
{ | ||
$match = []; | ||
|
||
if (!preg_match('#^(.+)?(\$[\S]+)\s*(.*)$#m', $tagDocblockLine, $match)) { | ||
return; | ||
} | ||
|
||
if ($match[1] !== '') { | ||
$this->types = explode('|', rtrim($match[1])); | ||
} | ||
|
||
if ($match[2] !== '') { | ||
$this->variableName = $match[2]; | ||
} | ||
|
||
if ($match[3] !== '') { | ||
$this->description = $match[3]; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getTypes() : array | ||
{ | ||
return $this->types; | ||
} | ||
|
||
public function getVariableName() : ?string | ||
{ | ||
return $this->variableName; | ||
} | ||
|
||
public function getDescription() : ?string | ||
{ | ||
return $this->description; | ||
} | ||
|
||
public function __toString() : string | ||
{ | ||
return 'DocBlock Tag [ * @' . $this->getName() . ' ]' . PHP_EOL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Code\Generator\DocBlock\Tag; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Zend\Code\Generator\DocBlock\Tag\VarTag; | ||
use Zend\Code\Generator\DocBlock\TagManager; | ||
use Zend\Code\Reflection\DocBlock\Tag\VarTag as ReflectionVarTag; | ||
use Zend\Code\Reflection\DocBlockReflection; | ||
|
||
/** | ||
* @covers \Zend\Code\Generator\DocBlock\Tag\VarTag | ||
*/ | ||
class VarTagTest extends TestCase | ||
{ | ||
/** | ||
* @var VarTag | ||
*/ | ||
private $tag; | ||
|
||
/** | ||
* @var TagManager | ||
*/ | ||
private $tagManager; | ||
|
||
protected function setUp() : void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->tag = new VarTag(); | ||
$this->tagManager = new TagManager(); | ||
|
||
$this->tagManager->initializeDefaultTags(); | ||
} | ||
|
||
public function testGetterAndSetterPersistValue() : void | ||
{ | ||
$tag = new VarTag('variable'); | ||
|
||
self::assertSame('variable', $tag->getVariableName()); | ||
} | ||
|
||
public function testGetterForVariableNameTrimsCorrectly() : void | ||
{ | ||
$this->tag->setVariableName('$variable$'); | ||
$this->assertEquals('variable$', $this->tag->getVariableName()); | ||
} | ||
|
||
public function testNameIsCorrect() : void | ||
{ | ||
$this->assertEquals('var', $this->tag->getName()); | ||
} | ||
|
||
public function testParamProducesCorrectDocBlockLine() : void | ||
{ | ||
$this->tag->setVariableName('variable'); | ||
$this->tag->setTypes('string[]'); | ||
$this->tag->setDescription('description'); | ||
$this->assertEquals('@var string[] $variable description', $this->tag->generate()); | ||
} | ||
|
||
public function testConstructorWithOptions() : void | ||
{ | ||
$this->tag->setOptions([ | ||
'variableName' => 'foo', | ||
'types' => ['string'], | ||
'description' => 'description', | ||
]); | ||
$tagWithOptionsFromConstructor = new VarTag('foo', ['string'], 'description'); | ||
$this->assertEquals($this->tag->generate(), $tagWithOptionsFromConstructor->generate()); | ||
} | ||
|
||
public function testCreatingTagFromReflection() : void | ||
{ | ||
$reflectionTag = (new DocBlockReflection('/** @var int $foo description')) | ||
->getTag('var'); | ||
|
||
self::assertInstanceOf(ReflectionVarTag::class, $reflectionTag); | ||
|
||
/** @var VarTag $tag */ | ||
$tag = $this->tagManager->createTagFromReflection($reflectionTag); | ||
|
||
$this->assertInstanceOf(VarTag::class, $tag); | ||
$this->assertEquals('foo', $tag->getVariableName()); | ||
$this->assertEquals('description', $tag->getDescription()); | ||
$this->assertEquals('int', $tag->getTypesAsString()); | ||
} | ||
} |
Oops, something went wrong.