Completed
Push — master ( 0b395f...10fe71 )
by Andrii
03:11
created

TargetHydrator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * API for Billing
4
 *
5
 * @link      https://github.com/hiqdev/billing-hiapi
6
 * @package   billing-hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\billing\hiapi\target;
12
13
use hiqdev\billing\hiapi\models\Target;
14
use hiqdev\php\billing\target\TargetFactoryInterface;
15
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator;
16
use Zend\Hydrator\HydratorInterface;
17
18
/**
19
 * Class TargetHydrator.
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class TargetHydrator extends GeneratedHydrator
24
{
25
    /**
26
     * @var TargetFactoryInterface
27
     */
28
    private $targetFactory;
29
30 1
    public function __construct(HydratorInterface $hydrator, TargetFactoryInterface $targetFactory)
31
    {
32 1
        parent::__construct($hydrator);
33 1
        $this->targetFactory = $targetFactory;
34 1
    }
35
36
    /**
37
     * {@inheritdoc}
38
     * @param object|Target $object
39
     */
40
    public function extract($object)
41
    {
42
        $result = array_filter([
43
            'id'            => $object->getId(),
44
            'type'          => $object->getType(),
45
            'name'          => $object->getName(),
46
        ]);
47
48
        return $result;
49
    }
50
51
    /** {@inheritdoc} */
52 6 View Code Duplication
    public function hydrate(array $data, $object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54 6
        if (!empty($data['type'])) {
55 6
            $data['type'] = $this->targetFactory->shortenType($data['type']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface hiqdev\php\billing\target\TargetFactoryInterface as the method shortenType() does only exist in the following implementations of said interface: hiqdev\billing\hiapi\target\TargetFactory.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
56
        }
57
58 6
        return parent::hydrate($data, $object);
59
    }
60
61 5
    public function createEmptyInstance(string $className, array $data = [])
62
    {
63 5
        if (isset($data['type'])) {
64 5
            $className = $this->targetFactory->getClassForType($data['type']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface hiqdev\php\billing\target\TargetFactoryInterface as the method getClassForType() does only exist in the following implementations of said interface: hiqdev\billing\hiapi\target\TargetFactory.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
65
        }
66
67 5
        return parent::createEmptyInstance($className, $data);
68
    }
69
}
70