Ajde_Crud_Field::getIsUnique()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
abstract class Ajde_Crud_Field extends Ajde_Object_Standard
4
{
5
    /**
6
     * @var Ajde_Crud
7
     */
8
    protected $_crud;
9
10
    /**
11
     * @var string
12
     */
13
    protected $_type;
14
15
    protected $_useSpan = 12;
16
    protected $_attributes = [];
17
18
    public function __construct(Ajde_Crud $crud, $fieldOptions)
19
    {
20
        $explode = explode('_', get_class($this));
21
        end($explode);
22
        $this->_type = strtolower(current($explode));
23
        $this->_crud = $crud;
24
25
        /* defaults */
26
        $this->_data = [
27
            'name'            => isset($fieldOptions['name']) ? $fieldOptions['name'] : false,
28
            'type'            => 'text',
29
            'length'          => 255,
30
            'default'         => '',
31
            'label'           => isset($fieldOptions['name']) ? ucfirst($fieldOptions['name']) : false,
32
            'isRequired'      => false,
33
            'isPK'            => false,
34
            'isAutoIncrement' => false,
35
            'isAutoUpdate'    => false,
36
        ];
37
38
        /* options */
39
        foreach ($fieldOptions as $key => $value) {
40
            $this->set($key, $value);
41
        }
42
43
        $this->prepare();
44
    }
45
46
    protected function prepare()
47
    {
48
    }
49
50
    /**
51
     * Getters and setters.
52
     */
53
    public function getName()
54
    {
55
        return parent::getName();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajde_Object_Standard as the method getName() does only exist in the following sub-classes of Ajde_Object_Standard: Ajde_Crud_Field, Ajde_Crud_Field_Boolean, Ajde_Crud_Field_Date, Ajde_Crud_Field_Enum, Ajde_Crud_Field_File, Ajde_Crud_Field_Fk, Ajde_Crud_Field_Header, Ajde_Crud_Field_I18n, Ajde_Crud_Field_Icon, Ajde_Crud_Field_Media, Ajde_Crud_Field_Multiple, Ajde_Crud_Field_Numeric, Ajde_Crud_Field_Publish, Ajde_Crud_Field_Sort, Ajde_Crud_Field_Spatial, Ajde_Crud_Field_Text, Ajde_Crud_Field_Time, Ajde_Crud_Field_Timespan, Ajde_Layout, Ajde_Model_ValidatorAbstract, Ajde_Model_Validator_Date, Ajde_Model_Validator_Enum, Ajde_Model_Validator_HasChildren, Ajde_Model_Validator_Numeric, Ajde_Model_Validator_Required, Ajde_Model_Validator_Spatial, Ajde_Model_Validator_Text, Ajde_Model_Validator_Unique, Ajde_Shop_Transaction_Provider, Ajde_Shop_Transaction_Provider_Creditcard, Ajde_Shop_Transaction_Provider_Iban, Ajde_Shop_Transaction_Provider_Mollie, Ajde_Shop_Transaction_Provider_Mollie_Ideal, Ajde_Shop_Transaction_Provider_Paypal, Ajde_Shop_Transaction_Provider_Paypal_Creditcard, Ajde_Shop_Transaction_Provider_Test, Ajde_Shop_Transaction_Provider_Wedeal. 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...
56
    }
57
58
    public function getDbType()
59
    {
60
        return parent::getDbType();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajde_Object_Standard as the method getDbType() does only exist in the following sub-classes of Ajde_Object_Standard: Ajde_Crud_Field, Ajde_Crud_Field_Boolean, Ajde_Crud_Field_Date, Ajde_Crud_Field_Enum, Ajde_Crud_Field_File, Ajde_Crud_Field_Fk, Ajde_Crud_Field_Header, Ajde_Crud_Field_I18n, Ajde_Crud_Field_Icon, Ajde_Crud_Field_Media, Ajde_Crud_Field_Multiple, Ajde_Crud_Field_Numeric, Ajde_Crud_Field_Publish, Ajde_Crud_Field_Sort, Ajde_Crud_Field_Spatial, Ajde_Crud_Field_Text, Ajde_Crud_Field_Time, Ajde_Crud_Field_Timespan, Ajde_Model_ValidatorAbstract, Ajde_Model_Validator_Date, Ajde_Model_Validator_Enum, Ajde_Model_Validator_HasChildren, Ajde_Model_Validator_Numeric, Ajde_Model_Validator_Required, Ajde_Model_Validator_Spatial, Ajde_Model_Validator_Text, Ajde_Model_Validator_Unique. 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...
61
    }
62
63
    public function getLabel()
64
    {
65
        return parent::getLabel();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajde_Object_Standard as the method getLabel() does only exist in the following sub-classes of Ajde_Object_Standard: Ajde_Crud_Field, Ajde_Crud_Field_Boolean, Ajde_Crud_Field_Date, Ajde_Crud_Field_Enum, Ajde_Crud_Field_File, Ajde_Crud_Field_Fk, Ajde_Crud_Field_Header, Ajde_Crud_Field_I18n, Ajde_Crud_Field_Icon, Ajde_Crud_Field_Media, Ajde_Crud_Field_Multiple, Ajde_Crud_Field_Numeric, Ajde_Crud_Field_Publish, Ajde_Crud_Field_Sort, Ajde_Crud_Field_Spatial, Ajde_Crud_Field_Text, Ajde_Crud_Field_Time, Ajde_Crud_Field_Timespan, Ajde_Model_ValidatorAbstract, Ajde_Model_Validator_Date, Ajde_Model_Validator_Enum, Ajde_Model_Validator_HasChildren, Ajde_Model_Validator_Numeric, Ajde_Model_Validator_Required, Ajde_Model_Validator_Spatial, Ajde_Model_Validator_Text, Ajde_Model_Validator_Unique. 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...
66
    }
67
68
    public function getLength()
69
    {
70
        return parent::getLength();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajde_Object_Standard as the method getLength() does only exist in the following sub-classes of Ajde_Object_Standard: Ajde_Crud_Field, Ajde_Crud_Field_Boolean, Ajde_Crud_Field_Date, Ajde_Crud_Field_Enum, Ajde_Crud_Field_File, Ajde_Crud_Field_Fk, Ajde_Crud_Field_Header, Ajde_Crud_Field_I18n, Ajde_Crud_Field_Icon, Ajde_Crud_Field_Media, Ajde_Crud_Field_Multiple, Ajde_Crud_Field_Numeric, Ajde_Crud_Field_Publish, Ajde_Crud_Field_Sort, Ajde_Crud_Field_Spatial, Ajde_Crud_Field_Text, Ajde_Crud_Field_Time, Ajde_Crud_Field_Timespan, Ajde_Model_ValidatorAbstract, Ajde_Model_Validator_Date, Ajde_Model_Validator_Enum, Ajde_Model_Validator_HasChildren, Ajde_Model_Validator_Numeric, Ajde_Model_Validator_Required, Ajde_Model_Validator_Spatial, Ajde_Model_Validator_Text, Ajde_Model_Validator_Unique. 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...
71
    }
72
73
    public function getIsRequired()
74
    {
75
        return parent::getIsRequired();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajde_Object_Standard as the method getIsRequired() does only exist in the following sub-classes of Ajde_Object_Standard: Ajde_Crud_Field, Ajde_Crud_Field_Boolean, Ajde_Crud_Field_Date, Ajde_Crud_Field_Enum, Ajde_Crud_Field_File, Ajde_Crud_Field_Fk, Ajde_Crud_Field_Header, Ajde_Crud_Field_I18n, Ajde_Crud_Field_Icon, Ajde_Crud_Field_Media, Ajde_Crud_Field_Multiple, Ajde_Crud_Field_Numeric, Ajde_Crud_Field_Publish, Ajde_Crud_Field_Sort, Ajde_Crud_Field_Spatial, Ajde_Crud_Field_Text, Ajde_Crud_Field_Time, Ajde_Crud_Field_Timespan, Ajde_Model_ValidatorAbstract, Ajde_Model_Validator_Date, Ajde_Model_Validator_Enum, Ajde_Model_Validator_HasChildren, Ajde_Model_Validator_Numeric, Ajde_Model_Validator_Required, Ajde_Model_Validator_Spatial, Ajde_Model_Validator_Text, Ajde_Model_Validator_Unique. 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...
76
    }
77
78
    public function getDefault()
79
    {
80
        return parent::getDefault();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajde_Object_Standard as the method getDefault() does only exist in the following sub-classes of Ajde_Object_Standard: Ajde_Crud_Field, Ajde_Crud_Field_Boolean, Ajde_Crud_Field_Date, Ajde_Crud_Field_Enum, Ajde_Crud_Field_File, Ajde_Crud_Field_Fk, Ajde_Crud_Field_Header, Ajde_Crud_Field_I18n, Ajde_Crud_Field_Icon, Ajde_Crud_Field_Media, Ajde_Crud_Field_Multiple, Ajde_Crud_Field_Numeric, Ajde_Crud_Field_Publish, Ajde_Crud_Field_Sort, Ajde_Crud_Field_Spatial, Ajde_Crud_Field_Text, Ajde_Crud_Field_Time, Ajde_Crud_Field_Timespan, Ajde_Model_ValidatorAbstract, Ajde_Model_Validator_Date, Ajde_Model_Validator_Enum, Ajde_Model_Validator_HasChildren, Ajde_Model_Validator_Numeric, Ajde_Model_Validator_Required, Ajde_Model_Validator_Spatial, Ajde_Model_Validator_Text, Ajde_Model_Validator_Unique. 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...
81
    }
82
83
    public function getIsAutoIncrement()
84
    {
85
        return parent::getIsAutoIncrement();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajde_Object_Standard as the method getIsAutoIncrement() does only exist in the following sub-classes of Ajde_Object_Standard: Ajde_Crud_Field, Ajde_Crud_Field_Boolean, Ajde_Crud_Field_Date, Ajde_Crud_Field_Enum, Ajde_Crud_Field_File, Ajde_Crud_Field_Fk, Ajde_Crud_Field_Header, Ajde_Crud_Field_I18n, Ajde_Crud_Field_Icon, Ajde_Crud_Field_Media, Ajde_Crud_Field_Multiple, Ajde_Crud_Field_Numeric, Ajde_Crud_Field_Publish, Ajde_Crud_Field_Sort, Ajde_Crud_Field_Spatial, Ajde_Crud_Field_Text, Ajde_Crud_Field_Time, Ajde_Crud_Field_Timespan, Ajde_Model_ValidatorAbstract, Ajde_Model_Validator_Date, Ajde_Model_Validator_Enum, Ajde_Model_Validator_HasChildren, Ajde_Model_Validator_Numeric, Ajde_Model_Validator_Required, Ajde_Model_Validator_Spatial, Ajde_Model_Validator_Text, Ajde_Model_Validator_Unique. 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...
86
    }
87
88
    public function getIsAutoUpdate()
89
    {
90
        return parent::getIsAutoUpdate();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajde_Object_Standard as the method getIsAutoUpdate() does only exist in the following sub-classes of Ajde_Object_Standard: Ajde_Crud_Field, Ajde_Crud_Field_Boolean, Ajde_Crud_Field_Date, Ajde_Crud_Field_Enum, Ajde_Crud_Field_File, Ajde_Crud_Field_Fk, Ajde_Crud_Field_Header, Ajde_Crud_Field_I18n, Ajde_Crud_Field_Icon, Ajde_Crud_Field_Media, Ajde_Crud_Field_Multiple, Ajde_Crud_Field_Numeric, Ajde_Crud_Field_Publish, Ajde_Crud_Field_Sort, Ajde_Crud_Field_Spatial, Ajde_Crud_Field_Text, Ajde_Crud_Field_Time, Ajde_Crud_Field_Timespan. 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...
91
    }
92
93
    public function getIsUnique()
94
    {
95
        return parent::getIsUnique();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajde_Object_Standard as the method getIsUnique() does only exist in the following sub-classes of Ajde_Object_Standard: Ajde_Crud_Field, Ajde_Crud_Field_Boolean, Ajde_Crud_Field_Date, Ajde_Crud_Field_Enum, Ajde_Crud_Field_File, Ajde_Crud_Field_Fk, Ajde_Crud_Field_Header, Ajde_Crud_Field_I18n, Ajde_Crud_Field_Icon, Ajde_Crud_Field_Media, Ajde_Crud_Field_Multiple, Ajde_Crud_Field_Numeric, Ajde_Crud_Field_Publish, Ajde_Crud_Field_Sort, Ajde_Crud_Field_Spatial, Ajde_Crud_Field_Text, Ajde_Crud_Field_Time, Ajde_Crud_Field_Timespan, Ajde_Model_ValidatorAbstract, Ajde_Model_Validator_Date, Ajde_Model_Validator_Enum, Ajde_Model_Validator_HasChildren, Ajde_Model_Validator_Numeric, Ajde_Model_Validator_Required, Ajde_Model_Validator_Spatial, Ajde_Model_Validator_Text, Ajde_Model_Validator_Unique. 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...
96
    }
97
98
    public function getValue()
99
    {
100
        if (parent::hasValue()) {
0 ignored issues
show
Bug introduced by
The method hasValue() does not seem to exist on object<Ajde_Object_Standard>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (hasValue() instead of getValue()). Are you sure this is correct? If so, you might want to change this to $this->hasValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
101
            return parent::getValue();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajde_Object_Standard as the method getValue() does only exist in the following sub-classes of Ajde_Object_Standard: Ajde_Crud_Field, Ajde_Crud_Field_Boolean, Ajde_Crud_Field_Date, Ajde_Crud_Field_Enum, Ajde_Crud_Field_File, Ajde_Crud_Field_Fk, Ajde_Crud_Field_Header, Ajde_Crud_Field_I18n, Ajde_Crud_Field_Icon, Ajde_Crud_Field_Media, Ajde_Crud_Field_Multiple, Ajde_Crud_Field_Numeric, Ajde_Crud_Field_Publish, Ajde_Crud_Field_Sort, Ajde_Crud_Field_Spatial, Ajde_Crud_Field_Text, Ajde_Crud_Field_Time, Ajde_Crud_Field_Timespan, Ajde_Model_ValidatorAbstract, Ajde_Model_Validator_Date, Ajde_Model_Validator_Enum, Ajde_Model_Validator_HasChildren, Ajde_Model_Validator_Numeric, Ajde_Model_Validator_Required, Ajde_Model_Validator_Spatial, Ajde_Model_Validator_Text, Ajde_Model_Validator_Unique. 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...
102
        } else {
103
            return false;
104
        }
105
    }
106
107
    public function getType()
108
    {
109
        return $this->_type;
110
    }
111
112
    /**
113
     * Template functions.
114
     */
115
    public function getHtml()
116
    {
117
        $template = $this->_getFieldTemplate();
118
        $template->assign('field', $this);
119
120
        return $template->render();
121
    }
122
123
    public function getInput($id = null)
124
    {
125
        $template = $this->_getInputTemplate();
126
        $template->assign('field', $this);
127
        $template->assign('id', $id);
128
129
        return $template->render();
130
    }
131
132
    protected function _getFieldTemplate()
133
    {
134
        return $this->_getTemplate('field');
135
    }
136
137
    protected function _getInputTemplate()
138
    {
139
        return $this->_getTemplate('field/'.$this->_type);
140
    }
141
142
    protected function _getTemplate($action)
143
    {
144
        $template = null;
145
        if (Ajde_Template::exist(MODULE_DIR.'_core/', 'crud/'.$action) !== false) {
146
            $template = new Ajde_Template(MODULE_DIR.'_core/', 'crud/'.$action);
147
            Ajde::app()->getDocument()->autoAddResources($template);
0 ignored issues
show
Bug introduced by
The method autoAddResources() does not exist on Ajde_Document. Did you maybe mean addResource()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
148
        }
149 View Code Duplication
        if ($this->_hasCustomTemplate($action)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
150
            $base = $this->_getCustomTemplateBase();
151
            $action = $this->_getCustomTemplateAction($action);
152
            $template = new Ajde_Template($base, $action);
153
        }
154
        if ($template instanceof Ajde_Template) {
155
            return $template;
156
        } else {
157
            // TODO:
158
            throw new Ajde_Exception('No crud template found for field '.$action);
159
        }
160
    }
161
162 View Code Duplication
    protected function _hasCustomTemplate($action)
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...
163
    {
164
        $base = $this->_getCustomTemplateBase();
165
        $action = $this->_getCustomTemplateAction($action);
166
167
        return Ajde_Template::exist($base, $action) !== false;
168
    }
169
170
    protected function _getCustomTemplateBase()
171
    {
172
        return MODULE_DIR.$this->_crud->getCustomTemplateModule().'/';
173
    }
174
175
    protected function _getCustomTemplateAction($action)
176
    {
177
        return 'crud/'.(string) $this->_crud->getModel()->getTable().'/'.$action;
178
    }
179
180
    /**
181
     * HTML functions.
182
     */
183
    public function getHtmlRequired()
184
    {
185
        //return '<span class="required">*</span>';
186
        return '';
187
    }
188
189
    public function getHtmlPK()
190
    {
191
        return " <img src='".MEDIA_DIR."icons/16/key_login.png' style='vertical-align: middle;' title='Primary key' />";
192
    }
193
194
    public function getHtmlAttributesAsArray()
195
    {
196
        if (empty($this->_attributes)) {
197
            $attributes = [];
198
            if (method_exists($this, '_getHtmlAttributes')) {
199
                $attributes = $this->_getHtmlAttributes();
0 ignored issues
show
Bug introduced by
The method _getHtmlAttributes() does not exist on Ajde_Crud_Field. Did you maybe mean getHtml()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
200
            }
201
            $attributes['name'] = $this->getName();
202
            if (!array_key_exists('id', $attributes)) {
203
                $attributes['id'] = 'in_'.$this->getName();
204
            }
205
            if ($this->hasNotEmpty('class')) {
206
                if (array_key_exists('class', $attributes)) {
207
                    $attributes['class'] .= ' '.$this->getClass();
0 ignored issues
show
Documentation Bug introduced by
The method getClass does not exist on object<Ajde_Crud_Field>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
208
                } else {
209
                    $attributes['class'] = $this->getClass();
0 ignored issues
show
Documentation Bug introduced by
The method getClass does not exist on object<Ajde_Crud_Field>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
210
                }
211
            }
212
            if ($this->_useSpan !== false) {
213
                if (array_key_exists('class', $attributes)) {
214
                    $attributes['class'] .= ' span'.$this->_useSpan;
215
                } else {
216
                    $attributes['class'] = 'span'.$this->_useSpan;
217
                }
218
            }
219
            $this->_attributes = $attributes;
220
        }
221
222
        return $this->_attributes;
223
    }
224
225
    public function getHtmlAttribute($name)
226
    {
227
        $attributes = $this->getHtmlAttributesAsArray();
228
        if (isset($attributes[$name])) {
229
            return $attributes[$name];
230
        }
231
232
        return false;
233
    }
234
235
    public function getHtmlAttributes()
236
    {
237
        $attributes = $this->getHtmlAttributesAsArray();
238
239
        $text = '';
240
        foreach ($attributes as $k => $v) {
241
            if (strlen($v)) {
242
                $text .= $k.'="'.$v.'" ';
243
            }
244
        }
245
246
        return $text;
247
    }
248
249
    /**
250
     * @return Ajde_Crud
251
     */
252
    public function getCrud()
253
    {
254
        return $this->_crud;
255
    }
256
}
257