Column::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace TildBJ\Seeder\Domain\Model;
3
4
/***************************************************************
5
 *
6
 *  Copyright notice
7
 *
8
 *  (c) 2016 Dennis Römmich <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
use TildBJ\Seeder\Domain\Model\Column\OneToManyInterface;
29
30
/**
31
 * Class Column
32
 *
33
 * @package TildBJ\Seeder\Domain\Model\Column
34
 */
35
abstract class Column implements ColumnInterface
0 ignored issues
show
Coding Style introduced by
Column does not seem to conform to the naming convention (^Abstract|Factory$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
36
{
37
38
    /**
39
     * @var string
40
     */
41
    protected $name;
42
43
    /**
44
     * Column constructor.
45
     *
46
     * @param string $name
47
     */
48
    public function __construct($name)
49
    {
50
        $this->name = $name;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getName()
57
    {
58
        return $this->name;
59
    }
60
61
    /**
62
     * isForeignKey
63
     *
64
     * @return bool
65
     */
66
    public function isForeignKey()
67
    {
68
        if ($this instanceof OneToManyInterface && $this->getForeignTable() !== null) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class TildBJ\Seeder\Domain\Model\Column as the method getForeignTable() does only exist in the following sub-classes of TildBJ\Seeder\Domain\Model\Column: TildBJ\Seeder\Domain\Model\Column\Group, TildBJ\Seeder\Domain\Model\Column\Inline, TildBJ\Seeder\Domain\Model\Column\Select. 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...
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $this instanceof ...oreignTable() !== null;.
Loading history...
69
            return true;
70
        }
71
        return false;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function __toString()
78
    {
79
        $reflectionClass = new \ReflectionClass($this);
80
81
        return $reflectionClass->getShortName();
82
    }
83
}
84