| Conditions | 25 |
| Paths | 4612 |
| Total Lines | 117 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 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 |
||
| 194 | protected static function findSource() |
||
| 195 | { |
||
| 196 | $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT); |
||
| 197 | |||
| 198 | // Not relevant to determine source |
||
| 199 | $internalClasses = [ |
||
| 200 | '', |
||
| 201 | get_called_class(), |
||
| 202 | // DebugBar |
||
| 203 | DebugBar::class, |
||
| 204 | \LeKoala\DebugBar\Middleware\DebugBarMiddleware::class, |
||
| 205 | // Proxy |
||
| 206 | ProxyDBExtension::class, |
||
| 207 | \TractorCow\ClassProxy\Proxied\ProxiedBehaviour::class, |
||
| 208 | // Orm |
||
| 209 | \SilverStripe\ORM\Connect\Database::class, |
||
| 210 | \SilverStripe\ORM\Connect\DBSchemaManager::class, |
||
| 211 | \SilverStripe\ORM\Connect\MySQLDatabase::class, |
||
| 212 | \SilverStripe\ORM\Connect\MySQLSchemaManager::class, |
||
| 213 | \SilverStripe\ORM\DataObjectSchema::class, |
||
| 214 | \SilverStripe\ORM\DB::class, |
||
| 215 | \SilverStripe\ORM\Queries\SQLExpression::class, |
||
| 216 | \SilverStripe\ORM\DataList::class, |
||
| 217 | \SilverStripe\ORM\DataObject::class, |
||
| 218 | \SilverStripe\ORM\DataQuery::class, |
||
| 219 | \SilverStripe\ORM\Queries\SQLSelect::class, |
||
| 220 | \SilverStripe\ORM\Map::class, |
||
| 221 | \SilverStripe\ORM\ListDecorator::class, |
||
| 222 | // Core |
||
| 223 | \SilverStripe\Control\Director::class, |
||
| 224 | ]; |
||
| 225 | |||
| 226 | $viewerClasses = [ |
||
| 227 | \SilverStripe\View\SSViewer_DataPresenter::class, |
||
| 228 | \SilverStripe\View\SSViewer_Scope::class, |
||
| 229 | \SilverStripe\View\SSViewer::class, |
||
| 230 | \LeKoala\DebugBar\Proxy\SSViewerProxy::class, |
||
| 231 | \SilverStripe\View\ViewableData::class |
||
| 232 | ]; |
||
| 233 | |||
| 234 | $sources = []; |
||
| 235 | foreach ($traces as $i => $trace) { |
||
| 236 | // We need to be able to look ahead one item in the trace, because the class/function values |
||
| 237 | // are talking about what is being *called* on this line, not the function this line lives in. |
||
| 238 | if (!isset($traces[$i + 1])) { |
||
| 239 | break; |
||
| 240 | } |
||
| 241 | |||
| 242 | $file = isset($trace['file']) ? pathinfo($trace['file'], PATHINFO_FILENAME) : null; |
||
| 243 | $class = isset($traces[$i + 1]['class']) ? $traces[$i + 1]['class'] : null; |
||
| 244 | $line = isset($trace['line']) ? $trace['line'] : null; |
||
| 245 | $function = isset($traces[$i + 1]['function']) ? $traces[$i + 1]['function'] : null; |
||
| 246 | $type = isset($traces[$i + 1]['type']) ? $traces[$i + 1]['type'] : '::'; |
||
| 247 | |||
| 248 | /* @var $object SSViewer */ |
||
| 249 | $object = isset($traces[$i + 1]['object']) ? $traces[$i + 1]['object'] : null; |
||
| 250 | |||
| 251 | if (in_array($class, $internalClasses)) { |
||
| 252 | continue; |
||
| 253 | } |
||
| 254 | |||
| 255 | // Viewer classes need special handling |
||
| 256 | if (in_array($class, $viewerClasses)) { |
||
| 257 | if ($function == 'includeGeneratedTemplate') { |
||
| 258 | $templates = $object->templates(); |
||
| 259 | |||
| 260 | $template = null; |
||
| 261 | if (isset($templates['main'])) { |
||
| 262 | $template = basename($templates['main']); |
||
| 263 | } else { |
||
| 264 | $keys = array_keys($templates); |
||
| 265 | $key = reset($keys); |
||
| 266 | if (isset($templates[$key])) { |
||
| 267 | $template = $key . ':' . basename($templates[$key]); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | if (!empty($template)) { |
||
| 271 | $sources[] = $template; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | continue; |
||
| 275 | } |
||
| 276 | |||
| 277 | $name = $class; |
||
| 278 | if ($class && !DebugBar::config()->get('show_namespaces')) { |
||
| 279 | $nameArray = explode("\\", $class); |
||
| 280 | $name = array_pop($nameArray); |
||
| 281 | |||
| 282 | // Maybe we are inside a trait? |
||
| 283 | if ($file && $file != $name) { |
||
| 284 | $name .= '(' . $file . ')'; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | if ($function) { |
||
| 288 | $name .= $type . $function; |
||
| 289 | } |
||
| 290 | if ($line) { |
||
| 291 | // Line number could apply to a trait |
||
| 292 | $name .= ':' . $line; |
||
| 293 | } |
||
| 294 | |||
| 295 | $sources[] = $name; |
||
| 296 | |||
| 297 | if (count($sources) > self::MAX_FIND_SOURCE_LEVEL) { |
||
| 298 | break; |
||
| 299 | } |
||
| 300 | |||
| 301 | // We reached a Controller, exit loop |
||
| 302 | if ($object && $object instanceof Controller) { |
||
| 303 | break; |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | if (empty($sources)) { |
||
| 308 | return 'Undefined source'; |
||
| 309 | } |
||
| 310 | return implode(' > ', $sources); |
||
| 311 | } |
||
| 313 |