AbstractColumn::getTables()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace donatj\MySqlSchema\Columns;
4
5
use donatj\MySqlSchema\Columns\Interfaces\CharsetColumnInterface;
6
use donatj\MySqlSchema\Columns\Interfaces\OptionalLengthInterface;
7
use donatj\MySqlSchema\Columns\Interfaces\RequiredLengthInterface;
8
use donatj\MySqlSchema\Columns\Interfaces\SignedInterface;
9
use donatj\MySqlSchema\Columns\Numeric\AbstractIntegerColumn;
10
use donatj\MySqlSchema\Table;
11
use donatj\MySqlSchema\Traits\EscapeTrait;
12
13
abstract class AbstractColumn {
14
15
	use EscapeTrait;
16
17
	/**
18
	 * @var \donatj\MySqlSchema\Table[]
19
	 */
20
	protected $tables = [ ];
21
	/**
22
	 * @var string
23
	 */
24
	protected $name;
25
	/**
26
	 * @var string
27
	 */
28
	protected $comment = '';
29
	/**
30
	 * @var bool
31
	 */
32
	protected $nullable = false;
33
	/**
34
	 * @var mixed
35
	 */
36
	protected $default;
37
38
	/**
39
	 * @param string $name
40
	 */
41
	public function __construct( $name ) {
42
		$this->name = $name;
43
	}
44
45
	/**
46
	 * @access private
47
	 * @param \donatj\MySqlSchema\Table $table
48
	 */
49
	public function addTable( Table $table ) {
50
		$this->tables[spl_object_hash($table)] = $table;
51
	}
52
53
	/**
54
	 * @return \donatj\MySqlSchema\Table[]
55
	 */
56
	public function getTables() {
57
		return array_values($this->tables);
58
	}
59
60
	/**
61
	 * @return string
62
	 */
63
	public function getComment() {
64
		return $this->comment;
65
	}
66
67
	/**
68
	 * @param string $comment
69
	 */
70
	public function setComment( $comment ) {
71
		$this->comment = $comment;
72
	}
73
74
	/**
75
	 * @return boolean
76
	 */
77
	public function isNullable() {
78
		return $this->nullable;
79
	}
80
81
	/**
82
	 * @param boolean $nullable
83
	 */
84
	public function setNullable( $nullable ) {
85
		$this->nullable = $nullable;
86
	}
87
88
	/**
89
	 * @return string
90
	 */
91
	public function getName() {
92
		return $this->name;
93
	}
94
95
	/**
96
	 * @param string $name
97
	 */
98
	public function setName( $name ) {
99
		$this->name = $name;
100
	}
101
102
	/**
103
	 * @param \donatj\MySqlSchema\Table $table
104
	 * @return string
105
	 */
106
	public function toString( Table $table ) {
107
		$type = $this->getTypeName();
108
109
		$nullable = '';
110
		if( !$this->nullable ) {
111
			$nullable = ' NOT NULL';
112
		}
113
114
		$length = '';
115
		if( $this instanceof RequiredLengthInterface ||
116
			($this instanceof OptionalLengthInterface && $this->getLength())
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class donatj\MySqlSchema\Columns\AbstractColumn as the method getLength() does only exist in the following sub-classes of donatj\MySqlSchema\Columns\AbstractColumn: donatj\MySqlSchema\Colum...\AbstractFractionColumn, donatj\MySqlSchema\Colum...c\AbstractIntegerColumn, donatj\MySqlSchema\Colum...ic\AbstractNumberColumn, donatj\MySqlSchema\Colum...ixedPoint\DecimalColumn, donatj\MySqlSchema\Colum...atingPoint\DoubleColumn, donatj\MySqlSchema\Colum...oatingPoint\FloatColumn, donatj\MySqlSchema\Colum...c\Integers\BigIntColumn, donatj\MySqlSchema\Colum...eric\Integers\IntColumn, donatj\MySqlSchema\Colum...ntegers\MediumIntColumn, donatj\MySqlSchema\Colum...Integers\SmallIntColumn, donatj\MySqlSchema\Colum...\Integers\TinyIntColumn, donatj\MySqlSchema\Colum...AbstractCharacterColumn, donatj\MySqlSchema\Colum...ng\Character\CharColumn, donatj\MySqlSchema\Colum...Character\VarcharColumn, donatj\MySqlSchema\Columns\Temporal\YearColumn. 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...
117
		) {
118
			$l      = intval($this->getLength());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class donatj\MySqlSchema\Columns\AbstractColumn as the method getLength() does only exist in the following sub-classes of donatj\MySqlSchema\Columns\AbstractColumn: donatj\MySqlSchema\Colum...\AbstractFractionColumn, donatj\MySqlSchema\Colum...c\AbstractIntegerColumn, donatj\MySqlSchema\Colum...ic\AbstractNumberColumn, donatj\MySqlSchema\Colum...ixedPoint\DecimalColumn, donatj\MySqlSchema\Colum...atingPoint\DoubleColumn, donatj\MySqlSchema\Colum...oatingPoint\FloatColumn, donatj\MySqlSchema\Colum...c\Integers\BigIntColumn, donatj\MySqlSchema\Colum...eric\Integers\IntColumn, donatj\MySqlSchema\Colum...ntegers\MediumIntColumn, donatj\MySqlSchema\Colum...Integers\SmallIntColumn, donatj\MySqlSchema\Colum...\Integers\TinyIntColumn, donatj\MySqlSchema\Colum...AbstractCharacterColumn, donatj\MySqlSchema\Colum...ng\Character\CharColumn, donatj\MySqlSchema\Colum...Character\VarcharColumn, donatj\MySqlSchema\Columns\Temporal\YearColumn. 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...
119
			$length = "($l)";
120
		}
121
122
		$signed = '';
123
		if( $this instanceof SignedInterface ) {
124
			if( !$this->isSigned() ) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class donatj\MySqlSchema\Columns\AbstractColumn as the method isSigned() does only exist in the following sub-classes of donatj\MySqlSchema\Columns\AbstractColumn: donatj\MySqlSchema\Colum...\AbstractFractionColumn, donatj\MySqlSchema\Colum...c\AbstractIntegerColumn, donatj\MySqlSchema\Colum...ic\AbstractNumberColumn, donatj\MySqlSchema\Colum...ixedPoint\DecimalColumn, donatj\MySqlSchema\Colum...atingPoint\DoubleColumn, donatj\MySqlSchema\Colum...oatingPoint\FloatColumn, donatj\MySqlSchema\Colum...c\Integers\BigIntColumn, donatj\MySqlSchema\Colum...eric\Integers\IntColumn, donatj\MySqlSchema\Colum...ntegers\MediumIntColumn, donatj\MySqlSchema\Colum...Integers\SmallIntColumn, donatj\MySqlSchema\Colum...\Integers\TinyIntColumn. 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...
125
				$signed = " unsigned";
126
			}
127
		}
128
129
		$default = '';
130
		if( !is_null($this->default) ) {
131
			$default = ' DEFAULT ' . $this->mkString($this->default, "'");;
132
		}
133
134
		$charset   = '';
135
		$collation = '';
136 View Code Duplication
		if( $this instanceof CharsetColumnInterface ) {
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...
137
			if( $this->getCharset() ) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class donatj\MySqlSchema\Columns\AbstractColumn as the method getCharset() does only exist in the following sub-classes of donatj\MySqlSchema\Columns\AbstractColumn: donatj\MySqlSchema\Colum...AbstractCharacterColumn, donatj\MySqlSchema\Colum...ng\AbstractStringColumn, donatj\MySqlSchema\Colum...ring\AbstractTextColumn, donatj\MySqlSchema\Colum...ng\Character\CharColumn, donatj\MySqlSchema\Colum...Character\VarcharColumn, donatj\MySqlSchema\Colum...ing\Text\LongTextColumn, donatj\MySqlSchema\Colum...g\Text\MediumTextColumn, donatj\MySqlSchema\Columns\String\Text\TextColumn, donatj\MySqlSchema\Colum...ing\Text\TinyTextColumn. 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...
138
				$charset = ' CHARACTER SET ' . $this->getCharset();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class donatj\MySqlSchema\Columns\AbstractColumn as the method getCharset() does only exist in the following sub-classes of donatj\MySqlSchema\Columns\AbstractColumn: donatj\MySqlSchema\Colum...AbstractCharacterColumn, donatj\MySqlSchema\Colum...ng\AbstractStringColumn, donatj\MySqlSchema\Colum...ring\AbstractTextColumn, donatj\MySqlSchema\Colum...ng\Character\CharColumn, donatj\MySqlSchema\Colum...Character\VarcharColumn, donatj\MySqlSchema\Colum...ing\Text\LongTextColumn, donatj\MySqlSchema\Colum...g\Text\MediumTextColumn, donatj\MySqlSchema\Columns\String\Text\TextColumn, donatj\MySqlSchema\Colum...ing\Text\TinyTextColumn. 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...
139
				if( $this->getCollation() ) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class donatj\MySqlSchema\Columns\AbstractColumn as the method getCollation() does only exist in the following sub-classes of donatj\MySqlSchema\Columns\AbstractColumn: donatj\MySqlSchema\Colum...AbstractCharacterColumn, donatj\MySqlSchema\Colum...ng\AbstractStringColumn, donatj\MySqlSchema\Colum...ring\AbstractTextColumn, donatj\MySqlSchema\Colum...ng\Character\CharColumn, donatj\MySqlSchema\Colum...Character\VarcharColumn, donatj\MySqlSchema\Colum...ing\Text\LongTextColumn, donatj\MySqlSchema\Colum...g\Text\MediumTextColumn, donatj\MySqlSchema\Columns\String\Text\TextColumn, donatj\MySqlSchema\Colum...ing\Text\TinyTextColumn. 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...
140
					$collation = ' COLLATE ' . $this->getCollation();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class donatj\MySqlSchema\Columns\AbstractColumn as the method getCollation() does only exist in the following sub-classes of donatj\MySqlSchema\Columns\AbstractColumn: donatj\MySqlSchema\Colum...AbstractCharacterColumn, donatj\MySqlSchema\Colum...ng\AbstractStringColumn, donatj\MySqlSchema\Colum...ring\AbstractTextColumn, donatj\MySqlSchema\Colum...ng\Character\CharColumn, donatj\MySqlSchema\Colum...Character\VarcharColumn, donatj\MySqlSchema\Colum...ing\Text\LongTextColumn, donatj\MySqlSchema\Colum...g\Text\MediumTextColumn, donatj\MySqlSchema\Columns\String\Text\TextColumn, donatj\MySqlSchema\Colum...ing\Text\TinyTextColumn. 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...
141
				}
142
			}
143
		}
144
145
		$comment = '';
146
		if( $this->comment ) {
147
			$comment = ' COMMENT ' . $this->mkString($this->comment, "'");
148
		}
149
150
		$autoIncrement = '';
151
		if( $this instanceof AbstractIntegerColumn ) {
152
			if( $table->isAutoIncrement($this) ) {
0 ignored issues
show
Compatibility introduced by
$this of type object<donatj\MySqlSchema\Columns\AbstractColumn> is not a sub-type of object<donatj\MySqlSchem...\AbstractIntegerColumn>. It seems like you assume a child class of the class donatj\MySqlSchema\Columns\AbstractColumn to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
153
				$autoIncrement = ' AUTO_INCREMENT';
154
			}
155
		}
156
157
		$name = $this->mkString($this->name);
158
159
		return "{$name} {$type}{$length}{$signed}{$charset}{$collation}{$nullable}{$default}{$autoIncrement}{$comment}";
160
	}
161
162
	/**
163
	 * @return string
164
	 */
165
	abstract public function getTypeName();
166
167
	/**
168
	 * @return mixed
169
	 */
170
	public function getDefault() {
171
		return $this->default;
172
	}
173
174
	/**
175
	 * @param mixed $default
176
	 */
177
	public function setDefault( $default ) {
178
		$this->default = $default;
179
	}
180
}
181