Skip to content

Commit 5233457

Browse files
committed
Ensure - interface propagation
1 parent 85f0f4c commit 5233457

File tree

11 files changed

+194
-10
lines changed

11 files changed

+194
-10
lines changed

src/Aspect/PostconditionCheckerAspect.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Go\Aop\Aspect;
1515
use Go\Aop\Intercept\MethodInvocation;
1616
use PhpDeal\Annotation\Ensure;
17-
use PhpDeal\Contract\Fetcher\ParentClass\MethodConditionFetcher;
17+
use PhpDeal\Contract\Fetcher\Parent\MethodConditionFetcher;
1818
use PhpDeal\Exception\ContractViolation;
1919
use Go\Lang\Annotation\Around;
2020

src/Contract/Fetcher/ParentClass/AbstractFetcher.php src/Contract/Fetcher/Parent/AbstractFetcher.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* with this source code in the file LICENSE.
99
*/
1010

11-
namespace PhpDeal\Contract\Fetcher\ParentClass;
11+
namespace PhpDeal\Contract\Fetcher\Parent;
1212

1313
use Doctrine\Common\Annotations\Reader;
1414

src/Contract/Fetcher/ParentClass/MethodConditionFetcher.php src/Contract/Fetcher/Parent/MethodConditionFetcher.php

+32-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* with this source code in the file LICENSE.
99
*/
1010

11-
namespace PhpDeal\Contract\Fetcher\ParentClass;
11+
namespace PhpDeal\Contract\Fetcher\Parent;
1212

1313
use ReflectionClass;
1414

@@ -26,9 +26,9 @@ public function getConditions(ReflectionClass $class, $methodName)
2626
{
2727
$annotations = [];
2828
$parentMethods = [];
29-
while (($class = $class->getParentClass()) && $class->hasMethod($methodName)) {
30-
$parentMethods[] = $class->getMethod($methodName);
31-
}
29+
30+
$this->getParentClassMethods($class, $methodName, $parentMethods);
31+
$this->getInterfacesMethods($class, $methodName, $parentMethods);
3232

3333
foreach ($parentMethods as $parentMethod) {
3434
$annotations = array_merge($annotations, $this->annotationReader->getMethodAnnotations($parentMethod));
@@ -37,4 +37,32 @@ public function getConditions(ReflectionClass $class, $methodName)
3737

3838
return $contracts;
3939
}
40+
41+
/**
42+
* @param ReflectionClass $class
43+
* @param string $methodName
44+
* @param array $parentMethods
45+
*/
46+
private function getParentClassMethods(ReflectionClass $class, $methodName, &$parentMethods)
47+
{
48+
while (($class = $class->getParentClass()) && $class->hasMethod($methodName)) {
49+
$parentMethods[] = $class->getMethod($methodName);
50+
}
51+
}
52+
53+
/**
54+
* @param ReflectionClass $class
55+
* @param string $methodName
56+
* @param array $parentMethods
57+
*/
58+
private function getInterfacesMethods(ReflectionClass $class, $methodName, &$parentMethods)
59+
{
60+
$interfaces = $class->getInterfaces();
61+
62+
foreach ($interfaces as $interface) {
63+
if ($interface->hasMethod($methodName)) {
64+
$parentMethods[] = $interface->getMethod($methodName);
65+
}
66+
}
67+
}
4068
}

tests/Functional/Ensure/Propagation/ContractTest.php tests/Functional/Ensure/ClassPropagation/ContractTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* with this source code in the file LICENSE.
99
*/
1010

11-
namespace PhpDeal\Functional\Ensure\Propagation;
11+
namespace PhpDeal\Functional\Ensure\ClassPropagation;
1212

1313
/**
1414
* @group propagation

tests/Functional/Ensure/Propagation/Stub.php tests/Functional/Ensure/ClassPropagation/Stub.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* with this source code in the file LICENSE.
99
*/
1010

11-
namespace PhpDeal\Functional\Ensure\Propagation;
11+
namespace PhpDeal\Functional\Ensure\ClassPropagation;
1212

1313
use PhpDeal\Annotation as Contract;
1414

tests/Functional/Ensure/Propagation/StubGrandparent.php tests/Functional/Ensure/ClassPropagation/StubGrandparent.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* with this source code in the file LICENSE.
99
*/
1010

11-
namespace PhpDeal\Functional\Ensure\Propagation;
11+
namespace PhpDeal\Functional\Ensure\ClassPropagation;
1212

1313
use PhpDeal\Annotation as Contract;
1414

tests/Functional/Ensure/Propagation/StubParent.php tests/Functional/Ensure/ClassPropagation/StubParent.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* with this source code in the file LICENSE.
99
*/
1010

11-
namespace PhpDeal\Functional\Ensure\Propagation;
11+
namespace PhpDeal\Functional\Ensure\ClassPropagation;
1212

1313
use PhpDeal\Annotation as Contract;
1414

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* PHP Deal framework
4+
*
5+
* @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
6+
*
7+
* This source file is subject to the license that is bundled
8+
* with this source code in the file LICENSE.
9+
*/
10+
11+
namespace PhpDeal\Functional\Ensure\InterfacePropagation;
12+
13+
/**
14+
* @group propagation
15+
*/
16+
class ContractTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var Stub
20+
*/
21+
private $stub;
22+
23+
public function setUp()
24+
{
25+
parent::setUp();
26+
$this->stub = new Stub();
27+
}
28+
29+
public function tearDown()
30+
{
31+
unset($this->stub);
32+
parent::tearDown();
33+
}
34+
35+
public function providerEnsureValid()
36+
{
37+
return [
38+
[
39+
'variable' => 50,
40+
]
41+
];
42+
}
43+
44+
/**
45+
* @param int $variable
46+
* @dataProvider providerEnsureValid
47+
*/
48+
public function testEnsureValid($variable)
49+
{
50+
$this->stub->add($variable);
51+
}
52+
53+
public function providerEnsureInvalid()
54+
{
55+
return [
56+
[
57+
// invalid for Stub
58+
'variable' => 0
59+
],
60+
[
61+
// invalid for StubInterfaceA
62+
'variable' => -1
63+
],
64+
[
65+
// invalid for StubInterfaceB
66+
'variable' => -2
67+
],
68+
];
69+
}
70+
71+
/**
72+
* @param int $variable
73+
* @dataProvider providerEnsureInvalid
74+
* @expectedException \PhpDeal\Exception\ContractViolation
75+
*/
76+
public function testEnsureInvalid($variable)
77+
{
78+
$this->stub->add($variable);
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* PHP Deal framework
4+
*
5+
* @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
6+
*
7+
* This source file is subject to the license that is bundled
8+
* with this source code in the file LICENSE.
9+
*/
10+
11+
namespace PhpDeal\Functional\Ensure\InterfacePropagation;
12+
13+
use PhpDeal\Annotation as Contract;
14+
15+
class Stub implements StubInterfaceA, StubInterfaceB
16+
{
17+
/**
18+
* @var int
19+
*/
20+
private $value;
21+
22+
/**
23+
* @Contract\Ensure("$this->value !== 0")
24+
* @param int $variable
25+
*/
26+
public function add($variable)
27+
{
28+
$this->value += $variable;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* PHP Deal framework
5+
*
6+
* @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
7+
*
8+
* This source file is subject to the license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace PhpDeal\Functional\Ensure\InterfacePropagation;
13+
14+
use PhpDeal\Annotation as Contract;
15+
16+
interface StubInterfaceA
17+
{
18+
/**
19+
* @Contract\Ensure("$this->value !== -1")
20+
* @param int $variable
21+
*/
22+
public function add($variable);
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* PHP Deal framework
5+
*
6+
* @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
7+
*
8+
* This source file is subject to the license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace PhpDeal\Functional\Ensure\InterfacePropagation;
13+
14+
use PhpDeal\Annotation as Contract;
15+
16+
interface StubInterfaceB
17+
{
18+
/**
19+
* @Contract\Ensure("$this->value !== -2")
20+
* @param int $variable
21+
*/
22+
public function add($variable);
23+
}

0 commit comments

Comments
 (0)