Skip to content

Commit 56c4bea

Browse files
committed
Merge pull request #12 from pdaw/master
Little performance enhancement
2 parents bde278b + ba98106 commit 56c4bea

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/Contract/InvariantContract.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PhpDeal\Contract;
1212

13+
use Doctrine\Common\Annotations\Reader;
1314
use Go\Aop\Intercept\MethodInvocation;
1415
use PhpDeal\Contract\Fetcher\ParentClass\InvariantFetcher;
1516
use PhpDeal\Exception\ContractViolation;
@@ -18,6 +19,17 @@
1819

1920
class InvariantContract extends Contract
2021
{
22+
/**
23+
* @var InvariantFetcher
24+
*/
25+
private $invariantFetcher;
26+
27+
public function __construct(Reader $reader)
28+
{
29+
parent::__construct($reader);
30+
$this->invariantFetcher = new InvariantFetcher(Invariant::class);
31+
}
32+
2133
/**
2234
* Verifies invariants for contract class
2335
*
@@ -66,7 +78,7 @@ private function fetchAllContracts(ReflectionClass $class)
6678
*/
6779
private function fetchParentsContracts(ReflectionClass $class)
6880
{
69-
return (new InvariantFetcher(Invariant::class))->getConditions(
81+
return $this->invariantFetcher->getConditions(
7082
$class,
7183
$this->reader
7284
);

src/Contract/PostconditionContract.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,25 @@
1010

1111
namespace PhpDeal\Contract;
1212

13+
use Doctrine\Common\Annotations\Reader;
1314
use Go\Aop\Intercept\MethodInvocation;
1415
use PhpDeal\Contract\Fetcher\ParentClass\MethodConditionFetcher;
1516
use PhpDeal\Exception\ContractViolation;
1617
use PhpDeal\Annotation\Ensure;
1718

1819
class PostconditionContract extends Contract
1920
{
21+
/**
22+
* @var MethodConditionFetcher
23+
*/
24+
private $methodConditionFetcher;
25+
26+
public function __construct(Reader $reader)
27+
{
28+
parent::__construct($reader);
29+
$this->methodConditionFetcher = new MethodConditionFetcher(Ensure::class);
30+
}
31+
2032
/**
2133
* @param MethodInvocation $invocation
2234
* @throws ContractViolation
@@ -62,7 +74,7 @@ private function fetchAllContracts(MethodInvocation $invocation)
6274
*/
6375
private function fetchParentsContracts(MethodInvocation $invocation)
6476
{
65-
return (new MethodConditionFetcher(Ensure::class))->getConditions(
77+
return $this->methodConditionFetcher->getConditions(
6678
$invocation->getMethod()->getDeclaringClass(),
6779
$this->reader,
6880
$invocation->getMethod()->name

src/Contract/PreconditionContract.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,25 @@
1010

1111
namespace PhpDeal\Contract;
1212

13+
use Doctrine\Common\Annotations\Reader;
1314
use Go\Aop\Intercept\MethodInvocation;
1415
use PhpDeal\Contract\Fetcher\ParentClass\MethodConditionWithInheritDocFetcher;
1516
use PhpDeal\Exception\ContractViolation;
1617
use PhpDeal\Annotation\Verify;
1718

1819
class PreconditionContract extends Contract
1920
{
21+
/**
22+
* @var MethodConditionWithInheritDocFetcher
23+
*/
24+
private $methodConditionFetcher;
25+
26+
public function __construct(Reader $reader)
27+
{
28+
parent::__construct($reader);
29+
$this->methodConditionFetcher = new MethodConditionWithInheritDocFetcher(Verify::class);
30+
}
31+
2032
/**
2133
* @param MethodInvocation $invocation
2234
* @Before("@execution(PhpDeal\Annotation\Verify)")
@@ -55,7 +67,7 @@ private function fetchAllContracts(MethodInvocation $invocation)
5567
*/
5668
private function fetchParentsContracts(MethodInvocation $invocation)
5769
{
58-
return (new MethodConditionWithInheritDocFetcher(Verify::class))->getConditions(
70+
return $this->methodConditionFetcher->getConditions(
5971
$invocation->getMethod()->getDeclaringClass(),
6072
$this->reader,
6173
$invocation->getMethod()->name

0 commit comments

Comments
 (0)