Completed
Pull Request — master (#12)
by Sergey
02:44
created

Manager::preferences()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 14
c 1
b 0
f 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace seregazhuk\HeadHunterApi\EndPoints;
4
5
use seregazhuk\HeadHunterApi\Exceptions\HeadHunterApiException;
6
7
class Manager extends Endpoint
8
{
9
    const RESOURCE = '/employers';
10
11
    /**
12
     * Get manager settings
13
     * @param integer $managerId
14
     * @return array
15
     */
16
    public function preferences($managerId = null)
17
    {
18
        $employerId = $this->getCurrentEmployerId();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $employerId is correct as $this->getCurrentEmployerId() (which targets seregazhuk\HeadHunterApi...:getCurrentEmployerId()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
19
        $managerId = $managerId ? : $this->getCurrentManagerId();
20
21
        $uri = str_replace(
22
            ['{employer_id}', '{manager_id}'],
23
            [$employerId, $managerId],
24
            '{employer_id}/managers/{manager_id}/settings'
25
        );
26
27
28
        return $this->getResource($uri);
29
    }
30
31
    /**
32
     * @return null
33
     * @throws HeadHunterApiException
34
     */
35 View Code Duplication
    protected function getCurrentEmployerId()
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...
36
    {
37
        $currentUser = $this->getCurrentUserInfo();
38
39
        if(!isset($currentUser['employer']['id'])) {
40
            throw new HeadHunterApiException('Cannot resolve employer id');
41
        }
42
43
        return $currentUser['employer']['id'];
44
    }
45
46 View Code Duplication
    protected function getCurrentManagerId()
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...
47
    {
48
        $currentUser = $this->getCurrentUserInfo();
49
50
        if(!isset($currentUser['manager']['id'])) {
51
            throw new HeadHunterApiException('Cannot resolve manager id');
52
        }
53
54
        return $currentUser['manager']['id'];
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    protected function getCurrentUserInfo()
61
    {
62
        return $this->container->getEndpoint('me')->info();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class seregazhuk\HeadHunterApi\EndPoints\Endpoint as the method info() does only exist in the following sub-classes of seregazhuk\HeadHunterApi\EndPoints\Endpoint: seregazhuk\HeadHunterApi\EndPoints\Me. 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...
63
    }
64
}