Skip to content

real getters and setters for attributes #26

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

Merged
merged 1 commit into from
Jun 28, 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
12 changes: 12 additions & 0 deletions src/phpDocumentor/GraphViz/AttributeNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace phpDocumentor\GraphViz;

class AttributeNotFound extends Exception
{
public function __construct(string $name)
{
parent::__construct(sprintf('Attribute with name "%s" was not found.', $name));
}
}
29 changes: 29 additions & 0 deletions src/phpDocumentor/GraphViz/Attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace phpDocumentor\GraphViz;

trait Attributes
{
/** @var Attribute[] */
protected $attributes = [];

public function setAttribute(string $name, string $value): self
{
$this->attributes[$name] = new Attribute($name, $value);

return $this;
}

/**
* @throws AttributeNotFound
*/
public function getAttribute(string $name): Attribute
{
if (!array_key_exists($name, $this->attributes)) {
throw new AttributeNotFound($name);
}

return $this->attributes[$name];
}
}
15 changes: 7 additions & 8 deletions src/phpDocumentor/GraphViz/Edge.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@
*/
class Edge
{
use Attributes;

/** @var Node Node from where to link */
protected $from = null;

/** @var Node Node where to to link */
protected $to = null;

/** @var Attribute[] List of attributes for this edge */
protected $attributes = [];

/**
* Creates a new Edge / Link between the given nodes.
*
Expand Down Expand Up @@ -87,23 +86,23 @@ public function getTo(): Node
* Set methods return this graph (fluent interface) whilst get methods
* return the attribute value.
*
* @param string $name name of the invoked method, expect it to be
* @param string $name name of the invoked method, expect it to be
* setX or getX.
* @param mixed[] $arguments Arguments for the setter, only 1 is expected: value
*
* @return Attribute|Edge|null
*
* @throws AttributeNotFound
*/
public function __call(string $name, array $arguments)
{
$key = strtolower(substr($name, 3));
if (strtolower(substr($name, 0, 3)) === 'set') {
$this->attributes[$key] = new Attribute($key, (string) $arguments[0]);

return $this;
return $this->setAttribute($key, (string) $arguments[0]);
}

if (strtolower(substr($name, 0, 3)) === 'get') {
return $this->attributes[$key];
return $this->getAttribute($key);
}

return null;
Expand Down
15 changes: 7 additions & 8 deletions src/phpDocumentor/GraphViz/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
*/
class Graph
{
use Attributes;

/** @var string Name of this graph */
protected $name = 'G';

Expand All @@ -48,9 +50,6 @@ class Graph
/** @var bool If the graph is strict then multiple edges are not allowed between the same pairs of nodes */
protected $strict = false;

/** @var Attribute[] A list of attributes for this Graph */
protected $attributes = [];

/** @var Graph[] A list of subgraphs for this Graph */
protected $graphs = [];

Expand Down Expand Up @@ -172,22 +171,22 @@ public function isStrict(): bool
* Set methods return this graph (fluent interface) whilst get methods
* return the attribute value.
*
* @param string $name Name of the method including get/set
* @param string $name Name of the method including get/set
* @param mixed[] $arguments The arguments, should be 1: the value
*
* @return Attribute|Graph|null
*
* @throws AttributeNotFound
*/
public function __call(string $name, array $arguments)
{
$key = strtolower(substr($name, 3));
if (strtolower(substr($name, 0, 3)) === 'set') {
$this->attributes[$key] = new Attribute($key, $arguments[0]);

return $this;
return $this->setAttribute($key, (string) $arguments[0]);
}

if (strtolower(substr($name, 0, 3)) === 'get') {
return $this->attributes[$key];
return $this->getAttribute($key);
}

return null;
Expand Down
14 changes: 7 additions & 7 deletions src/phpDocumentor/GraphViz/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
*/
class Node
{
use Attributes;

/** @var string Name for this node */
protected $name = '';

/** @var Attribute[] List of attributes for this node */
protected $attributes = [];

/**
* Creates a new node with name and optional label.
*
Expand Down Expand Up @@ -91,21 +90,22 @@ public function getName(): string
* Set methods return this graph (fluent interface) whilst get methods
* return the attribute value.
*
* @param string $name Method name; either getX or setX is expected.
* @param string $name Method name; either getX or setX is expected.
* @param mixed[] $arguments List of arguments; only 1 is expected for setX.
*
* @return Attribute|Node|null
*
* @throws AttributeNotFound
*/
public function __call(string $name, array $arguments)
{
$key = strtolower(substr($name, 3));
if (strtolower(substr($name, 0, 3)) === 'set') {
$this->attributes[$key] = new Attribute($key, (string) $arguments[0]);
return $this;
return $this->setAttribute($key, (string) $arguments[0]);
}

if (strtolower(substr($name, 0, 3)) === 'get') {
return $this->attributes[$key];
return $this->getAttribute($key);
}

return null;
Expand Down
17 changes: 17 additions & 0 deletions tests/phpDocumentor/GraphViz/Test/EdgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace phpDocumentor\GraphViz\Test;

use Mockery as m;
use phpDocumentor\GraphViz\AttributeNotFound;
use phpDocumentor\GraphViz\Edge;
use phpDocumentor\GraphViz\Node;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -108,6 +109,8 @@ public function testGetTo()
* for the remaining method calls
*
* @covers \phpDocumentor\GraphViz\Edge::__call
* @covers \phpDocumentor\GraphViz\Edge::setAttribute
* @covers \phpDocumentor\GraphViz\Edge::getAttribute
*/
public function testCall()
{
Expand All @@ -118,6 +121,20 @@ public function testCall()
$this->assertNull($fixture->someNonExcistingMethod());
}

/**
* @covers \phpDocumentor\GraphViz\Edge::getAttribute
* @covers \phpDocumentor\GraphViz\AttributeNotFound::__construct
*/
public function testGetNonExistingAttributeThrowsAttributeNotFound()
{
$fixture = new Edge(new Node('from'), new Node('to'));

$this->expectException(AttributeNotFound::class);
$this->expectExceptionMessage('Attribute with name "label" was not found');

$fixture->getLabel();
}

/**
* Tests whether the magic __toString method returns a well formatted string
* as specified in the DOT standard
Expand Down
15 changes: 15 additions & 0 deletions tests/phpDocumentor/GraphViz/Test/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace phpDocumentor\GraphViz\Test;

use Mockery as m;
use phpDocumentor\GraphViz\AttributeNotFound;
use phpDocumentor\GraphViz\Edge;
use phpDocumentor\GraphViz\Graph;
use phpDocumentor\GraphViz\Node;
Expand Down Expand Up @@ -198,6 +199,8 @@ public function testSetPath()

/**
* @covers \phpDocumentor\GraphViz\Graph::__call
* @covers \phpDocumentor\GraphViz\Graph::getAttribute
* @covers \phpDocumentor\GraphViz\Graph::setAttribute
*/
public function test__call()
{
Expand All @@ -206,6 +209,18 @@ public function test__call()
$this->assertSame('black', $this->fixture->getColor()->getValue());
}

/**
* @covers \phpDocumentor\GraphViz\Graph::getAttribute
* @covers \phpDocumentor\GraphViz\AttributeNotFound::__construct
*/
public function testGetNonExistingAttributeThrowsAttributeNotFound()
{
$this->expectException(AttributeNotFound::class);
$this->expectExceptionMessage('Attribute with name "color" was not found');

$this->fixture->getColor();
}

/**
* @covers \phpDocumentor\GraphViz\Graph::addGraph
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/phpDocumentor/GraphViz/Test/NodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace phpDocumentor\GraphViz\Test;

use phpDocumentor\GraphViz\AttributeNotFound;
use phpDocumentor\GraphViz\Node;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -102,6 +103,8 @@ public function testName()
* for the remaining method calls
*
* @covers \phpDocumentor\GraphViz\Node::__call
* @covers \phpDocumentor\GraphViz\Node::getAttribute
* @covers \phpDocumentor\GraphViz\Node::setAttribute
*/
public function testCall()
{
Expand All @@ -111,6 +114,18 @@ public function testCall()
$this->assertNull($this->fixture->someNonExistingMethod());
}

/**
* @covers \phpDocumentor\GraphViz\Node::getAttribute
* @covers \phpDocumentor\GraphViz\AttributeNotFound::__construct
*/
public function testGetNonExistingAttributeThrowsAttributeNotFound()
{
$this->expectException(AttributeNotFound::class);
$this->expectExceptionMessage('Attribute with name "fontname" was not found');

$this->fixture->getFontname();
}

/**
* Tests whether the magic __toString method returns a well formatted string
* as specified in the DOT standard
Expand Down