Conditions | 14 |
Paths | 576 |
Total Lines | 55 |
Code Lines | 33 |
Lines | 8 |
Ratio | 14.55 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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()) |
||
|
|||
117 | ) { |
||
118 | $l = intval($this->getLength()); |
||
119 | $length = "($l)"; |
||
120 | } |
||
121 | |||
122 | $signed = ''; |
||
123 | if( $this instanceof SignedInterface ) { |
||
124 | if( !$this->isSigned() ) { |
||
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 ) { |
|
137 | if( $this->getCharset() ) { |
||
138 | $charset = ' CHARACTER SET ' . $this->getCharset(); |
||
139 | if( $this->getCollation() ) { |
||
140 | $collation = ' COLLATE ' . $this->getCollation(); |
||
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) ) { |
||
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 | |||
181 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: