Completed
Push — master ( 8ea274...4857d6 )
by Vitaly
03:56
created

Field::valueFieldName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4286
cc 3
eloc 10
nc 3
nop 0
1
<?php
2
namespace samsoncms\api;
3
4
use samsonframework\orm\Condition;
5
use samsonframework\orm\QueryInterface;
6
7
/**
8
 * SamsonCMS additional field table entity class
9
 * @package samson\cms
10
 */
11
class Field extends \samson\activerecord\field
12
{
13
    /**
14
     * Find additional field database record by Name.
15
     * This is generic method that should be used in nested classes to find its
16
     * records by some its primary key value.
17
     *
18
     * @param QueryInterface $query Query object instance
19
     * @param string $name Additional field name
20
     * @param self $return Variable to return found database record
21
     * @return bool|null|self  Field instance or null if 3rd parameter not passed
22
     */
23 View Code Duplication
    public static function byName(QueryInterface $query, $name, self & $return = null)
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...
24
    {
25
        // Get field record by name column
26
        $return = static::oneByColumn($query, 'Name', $name);
27
28
        // If only one argument is passed - return null, otherwise bool
29
        return func_num_args() > 1 ? $return == null : $return;
30
    }
31
32
    /**
33
     * Find additional field database record by Name or ID.
34
     * This is generic method that should be used in nested classes to find its
35
     * records by some its primary key value.
36
     *
37
     * @param QueryInterface $query Query object instance
38
     * @param string $nameOrID Additional field name or identifier
39
     * @param self $return Variable to return found database record
40
     * @return bool|null|self  Field instance or null if 3rd parameter not passed
41
     */
42 View Code Duplication
    public static function byNameOrID(QueryInterface $query, $nameOrID, self & $return = null)
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...
43
    {
44
        // Create id or URL condition
45
        $idOrUrl = new Condition('OR');
46
        $idOrUrl->add('FieldID', $nameOrID)->add('Name', $field);
0 ignored issues
show
Bug introduced by
The variable $field does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
47
48
        // Perform query
49
        $return = $query->className('field')->cond($idOrUrl)->first();
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.

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...
50
51
        // If only one argument is passed - return null, otherwise bool
52
        return func_num_args() > 1 ? $return == null : $return;
53
    }
54
55
    /** @return string Get additional field value field name depending on its type */
56
    public function valueFieldName()
57
    {
58
        switch ($this->Type) {
0 ignored issues
show
Bug introduced by
The property Type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
            case 7:
60
                return 'numeric_value';
61
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
62
            case 6:
63
                return 'key_value';
64
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
65
            default:
66
                return 'Value';
67
        }
68
    }
69
}
70