Completed
Push — master ( db00dc...830fb6 )
by Vitaly
02:37
created

MaterialField::byFieldIDAndMaterialID()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 16
rs 9.4286
cc 2
eloc 12
nc 2
nop 5
1
<?php
2
namespace samsoncms\api;
3
4
use samsonframework\orm\QueryInterface;
5
6
/**
7
 * SamsonCMS additional field value table entity class
8
 * @package samson\cms
9
 */
10
class MaterialField extends \samson\activerecord\materialfield
11
{
12
    /**
13
     * Find additional field value database record by its material and field identifiers.
14
     * This is generic method that should be used in nested classes to find its
15
     * records by some its primary key value.
16
     *
17
     * @param QueryInterface $query Query object instance
18
     * @param string $materialID Material identifier
19
     * @param string $fieldID Additional field identifier
20
     * @param self $return Variable to return found database record
21
     * @param string $locale Locale identifier
22
     * @return bool|null|self  Field instance or null if 3rd parameter not passed
23
     */
24
    public static function byFieldIDAndMaterialID(
25
        QueryInterface $query,
26
        $materialID,
27
        $fieldID,
28
        self & $return = null,
29
        $locale = DEFAULT_LOCALE
30
    ) {
31
        $return = $query->className(__CLASS__)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface samsonframework\orm\QueryInterface as the method className() does only exist in the following implementations of said interface: samson\activerecord\dbQuery, samson\cms\CMSMaterialQuery.

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...
32
            ->cond('MaterialID', $materialID)
33
            ->cond('FieldID', $fieldID)
34
            ->cond('locale', $locale)
35
            ->first();
36
37
        // If only one argument is passed - return null, otherwise bool
38
        return func_num_args() > 1 ? $return == null : $return;
39
    }
40
}
41