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.
Completed
Push — master ( b5dfb6...a568a9 )
by Mario
40:05
created

InformationCollectionMapper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A mapToFormData() 0 26 3
A configureOptions() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Netgen\InformationCollection\Integration\RepositoryForms;
6
7
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
8
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
9
use eZ\Publish\API\Repository\Values\ValueObject;
10
use eZ\Publish\SPI\Search\Field;
11
use EzSystems\RepositoryForms\Data\Content\FieldData;
12
use EzSystems\RepositoryForms\Data\Mapper\FormDataMapperInterface;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
15
class InformationCollectionMapper implements FormDataMapperInterface
16
{
17
    /**
18
     * Maps a ValueObject from eZ content repository to a data usable as underlying form data (e.g. create/update struct).
19
     *
20
     * @param \eZ\Publish\API\Repository\Values\ValueObject|\eZ\Publish\API\Repository\Values\Content\Content $contentDraft
21
     * @param array $params
22
     *
23
     * @return InformationCollectionData
24
     */
25
    public function mapToFormData(ValueObject $contentDraft, array $params = [])
26
    {
27
        $optionsResolver = new OptionsResolver();
28
        $this->configureOptions($optionsResolver);
29
30
        $params = $optionsResolver->resolve($params);
31
        $languageCode = $params['languageCode'];
32
33
        $data = new InformationCollectionData(['contentDraft' => $contentDraft]);
34
        $data->initialLanguageCode = $languageCode;
35
36
        $fields = $contentDraft->getFieldsByLanguage($languageCode);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class eZ\Publish\API\Repository\Values\ValueObject as the method getFieldsByLanguage() does only exist in the following sub-classes of eZ\Publish\API\Repository\Values\ValueObject: eZ\Publish\API\Repository\Values\Content\Content, eZ\Publish\API\Repository\Values\User\User, eZ\Publish\API\Repository\Values\User\UserGroup, eZ\Publish\Core\REST\Client\Values\Content\Content, eZ\Publish\Core\REST\Client\Values\User\User, eZ\Publish\Core\REST\Client\Values\User\UserGroup, eZ\Publish\Core\Repository\Values\Content\Content, eZ\Publish\Core\Reposito...es\Content\ContentProxy, eZ\Publish\Core\Repository\Values\User\User, eZ\Publish\Core\Repository\Values\User\UserGroup. 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...
37
        /** @var FieldDefinition $fieldDef */
38
        foreach ($params['contentType']->fieldDefinitions as $fieldDef) {
39
            if ($fieldDef->isInfoCollector) {
40
                $field = $fields[$fieldDef->identifier];
41
                $data->addFieldData(new FieldData([
42
                    'fieldDefinition' => $fieldDef,
43
                    'field' => $field,
44
                    'value' => $field->value,
45
                ]));
46
            }
47
        }
48
49
        return $data;
50
    }
51
52
    private function configureOptions(OptionsResolver $optionsResolver)
53
    {
54
        $optionsResolver
55
            ->setRequired(['languageCode', 'contentType'])
56
            ->setAllowedTypes('contentType', ContentType::class);
57
    }
58
}
59