Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

WIP[3.0] Pull logic out of TraitUsageGenerator and into ImportGenerator #33

Closed
wants to merge 4 commits into from
Closed
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
61 changes: 59 additions & 2 deletions src/Generator/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class ClassGenerator extends AbstractGenerator
*/
protected $namespaceName = null;

/**
* @var array Array of string names
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be UseGenerator[], or UseStatement[]. Stringly typed will bite us back here

*/
protected $uses = [];

/**
* @var DocBlockGenerator
*/
Expand Down Expand Up @@ -663,7 +668,7 @@ public function getProperty($propertyName)
*/
public function addUse($use, $useAlias = null)
{
$this->traitUsageGenerator->addUse($use, $useAlias);
$this->uses[$use] = $useAlias;
return $this;
}

Expand All @@ -674,7 +679,59 @@ public function addUse($use, $useAlias = null)
*/
public function getUses()
{
return $this->traitUsageGenerator->getUses();
$uses = [];
foreach ($this->uses as $use => $useAlias) {
$uses[] = ! empty($useAlias) ? $use . ' as ' . $useAlias : $use;
};

return $uses;
}

/**
* @param string $use
* @return bool
*/
public function hasUse($use)
{
return array_key_exists($use, $this->uses);
}

/**
* @param string $methodName
* @return ClassGenerator
*/
public function removeUse($use)
{
if ($this->hasUse($use)) {
unset($this->uses[$use]);
}

return $this;
}

/**
* @param string $use
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter type

* @return bool
*/
public function hasUseAlias($use)
{
if ($this->hasUse($use)) {
return !empty($this->uses[$use]);
}

return false;
}

/**
* @param $use
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter type

* @return ClassGenerator
*/
public function removeUseAlias($use)
{
if ($this->hasUse($use)) {
$this->addUse($use);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add?

}
return $this;
}

/**
Expand Down
26 changes: 0 additions & 26 deletions src/Generator/TraitUsageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,11 @@ class TraitUsageGenerator extends AbstractGenerator
*/
protected $traitOverrides = [];

/**
* @var array Array of string names
*/
protected $uses = [];

public function __construct(ClassGenerator $classGenerator)
{
$this->classGenerator = $classGenerator;
}

/**
* @inherit Zend\Code\Generator\TraitUsageInterface
*/
public function addUse($use, $useAlias = null)
{
if (! empty($useAlias)) {
$use .= ' as ' . $useAlias;
}

$this->uses[$use] = $use;
return $this;
}

/**
* @inherit Zend\Code\Generator\TraitUsageInterface
*/
public function getUses()
{
return array_values($this->uses);
}

/**
* @inherit Zend\Code\Generator\TraitUsageInterface
*/
Expand Down
42 changes: 42 additions & 0 deletions test/Generator/ClassGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,48 @@ public function testAddOneUseWithAliasTwiceOnlyAddsOne()
$this->assertContains('use My\First\Use\Class as MyAlias;', $generated);
}

public function testHasUse()
{
$classGenerator = new ClassGenerator();
$classGenerator->addUse('My\First\Use\Class');
$classGenerator->addUse('My\Second\Use\Class', 'MyAlias');

$this->assertTrue($classGenerator->hasUse('My\First\Use\Class'));
$this->assertTrue($classGenerator->hasUse('My\Second\Use\Class'));
}

public function testRemoveUse()
{
$classGenerator = new ClassGenerator();
$classGenerator->addUse('My\First\Use\Class');
$classGenerator->addUse('My\Second\Use\Class', 'MyAlias');

$this->assertTrue($classGenerator->hasUse('My\First\Use\Class'));
$this->assertTrue($classGenerator->hasUse('My\Second\Use\Class'));
$classGenerator->removeUse('My\First\Use\Class');
$classGenerator->removeUse('My\Second\Use\Class');
$this->assertFalse($classGenerator->hasUse('My\First\Use\Class'));
$this->assertFalse($classGenerator->hasUse('My\Second\Use\Class'));
}

public function testHasUseAlias()
{
$classGenerator = new ClassGenerator();
$classGenerator->addUse('My\First\Use\Class');
$classGenerator->addUse('My\Second\Use\Class', 'MyAlias');
$this->assertFalse($classGenerator->hasUseAlias('My\First\Use\Class'));
$this->assertTrue($classGenerator->hasUseAlias('My\Second\Use\Class'));
}

public function testRemoveUseAlias()
{
$classGenerator = new ClassGenerator();
$classGenerator->addUse('My\First\Use\Class', 'MyAlias');
$this->assertTrue($classGenerator->hasUseAlias('My\First\Use\Class'));
$classGenerator->removeUseAlias('My\First\Use\Class');
$this->assertFalse($classGenerator->hasUseAlias('My\First\Use\Class'));
}

public function testCreateFromArrayWithDocBlockFromArray()
{
$classGenerator = ClassGenerator::fromArray([
Expand Down