GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

DetachedResource::postDehydrate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace prgTW\BaseCRM\Resource;
4
5
use prgTW\BaseCRM\Service\Behavior\CustomFieldsTrait;
6
7
class DetachedResource extends BaseResource
8
{
9
	/** {@inheritdoc} */
10
	protected function postDehydrate(array &$data)
11
	{
12
		parent::postDehydrate($data);
13
14
		if (false === in_array(CustomFieldsTrait::class, class_uses($this)))
15
		{
16
			return;
17
		}
18
19
		/** @var CustomFieldsTrait $this */
20
		$customFieldsKey        = $this->getCustomFieldsKey();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class prgTW\BaseCRM\Resource\DetachedResource as the method getCustomFieldsKey() does only exist in the following sub-classes of prgTW\BaseCRM\Resource\DetachedResource: prgTW\BaseCRM\Service\Detached\Lead, prgTW\BaseCRM\Service\Detached\Source. 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...
21
		$customFieldsCollection = $this->getCustomFields();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class prgTW\BaseCRM\Resource\DetachedResource as the method getCustomFields() does only exist in the following sub-classes of prgTW\BaseCRM\Resource\DetachedResource: prgTW\BaseCRM\Service\Detached\Lead, prgTW\BaseCRM\Service\Detached\Source. 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...
22
		if (null !== $customFieldsCollection)
23
		{
24
			$data[$customFieldsKey] = $customFieldsCollection->toArray();
25
		}
26
	}
27
}
28