Completed
Push — develop ( ab74f6...880247 )
by
unknown
10:28
created

ViewController::indexAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 12

Duplication

Lines 6
Ratio 23.08 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 6
loc 26
rs 8.8571
cc 3
eloc 12
nc 3
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Cv\Controller;
12
13
use Cv\Repository\Cv as CvRepository;
14
use Zend\Mvc\Controller\AbstractActionController;
15
use Zend\Http\PhpEnvironment\Response;
16
use Zend\I18n\Translator\TranslatorInterface as Translator;
17
18
/**
19
 * ${CARET}
20
 *
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @author Miroslav Fedeleš <[email protected]>
23
 */
24
class ViewController extends AbstractActionController
25
{
26
27
    /**
28
     * @var \Cv\Repository\Cv
29
     */
30
    private $repository;
31
    
32
    /**
33
     * @var Translator
34
     */
35
    private $translator;
36
37
    /**
38
     * @param CvRepository $repository
39
     * @param Translator $translator
40
     */
41
    public function __construct(CvRepository $repository, Translator $translator)
42
    {
43
        $this->repository = $repository;
44
        $this->translator = $translator;
45
    }
46
47
    public function indexAction()
48
    {
49
        /** @var string|null $id */
50
        $id = $this->params('id');
51
        $resume = $this->repository->find($id);
52
53 View Code Duplication
        if (!$resume) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
54
            $this->getResponse()->setStatusCode(Response::STATUS_CODE_404);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\ResponseInterface as the method setStatusCode() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Response, Zend\Http\Response, Zend\Http\Response\Stream.

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...
55
            return [
56
                'message' => sprintf($this->translator->translate('Resume with id "%s" not found'), $id)
57
            ];
58
        }
59
60
        /* @todo REMOVE THIS
61
         * @codeCoverageIgnoreStart */
62
        if (!$resume->getDateCreated()) {
63
            $resume->setDateCreated();
64
        }
65
        /* @codeCoverageIgnoreEnd */
66
67
        $this->acl($resume, 'view');
0 ignored issues
show
Documentation Bug introduced by
The method acl does not exist on object<Cv\Controller\ViewController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
68
69
        return [
70
            'resume' => $resume
71
        ];
72
    }
73
    
74
}