Completed
Pull Request — 1.x (#38)
by Akihito
02:20
created

TransactionalInterceptor::beginTransaction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of the Ray.AuraSqlModule package
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\AuraSqlModule;
8
9
use Aura\Sql\ExtendedPdoInterface;
10
use Ray\Aop\MethodInterceptor;
11
use Ray\Aop\MethodInvocation;
12
use Ray\AuraSqlModule\Annotation\Transactional;
13
use Ray\AuraSqlModule\Exception\RollbackException;
14
15
class TransactionalInterceptor implements MethodInterceptor
16
{
17
    /**
18
     * @var ExtendedPdoInterface
19
     */
20
    private $pdo;
21
22 4
    public function __construct(ExtendedPdoInterface $pdo = null)
23
    {
24 4
        $this->pdo = $pdo;
25 4
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 3
    public function invoke(MethodInvocation $invocation)
31
    {
32
        /* @var Transactional $transactional */
33 3
        $transactional = $invocation->getMethod()->getAnnotation(Transactional::class);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class ReflectionMethod as the method getAnnotation() does only exist in the following sub-classes of ReflectionMethod: Ray\Aop\ReflectionMethod. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
34 3
        if (count($transactional->value) > 0) {
0 ignored issues
show
Deprecated Code introduced by
The property Ray\AuraSqlModule\Annotation\Transactional::$value has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
35 3
            return (new PropTransaction)($invocation, $transactional);
36
        }
37
        if (! $this->pdo instanceof ExtendedPdoInterface) {
38
            return $invocation->proceed();
39
        }
40
        try {
41
            $this->pdo->beginTransaction();
42
            $result = $invocation->proceed();
43
            $this->pdo->commit();
44
        } catch (\PDOException $e) {
45
            $this->pdo->rollBack();
46
            throw new RollbackException($e->getMessage(), 0, $e);
47
        }
48
49
        return $result;
50
    }
51
}
52