Completed
Push — master ( 96c5e7...f7b5e7 )
by Vasily
02:18
created

ClassFinder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 73
Duplicated Lines 24.66 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 18
loc 73
rs 10
c 0
b 0
f 0
wmc 16
lcom 0
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getClassBasename() 0 8 2
A getNamespace() 0 4 1
C find() 18 38 13

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace PHPDaemon\Core;
4
5
/**
6
 * Class finder
7
 *
8
 * @package Core
9
 *
10
 * @author  Vasily Zorin <[email protected]>
11
 */
12
class ClassFinder
13
{
14
    use \PHPDaemon\Traits\ClassWatchdog;
15
    use \PHPDaemon\Traits\StaticObjectWatchdog;
16
17
    /**
18
     * Get base class name of the given class or object
19
     * @param string|object $class Object or String
20
     * @return string Class
21
     */
22
    public static function getClassBasename($class)
23
    {
24
        if (is_object($class)) {
25
            $class = get_class($class);
26
        }
27
        $e = explode('\\', $class);
28
        return end($e);
29
    }
30
31
    /**
32
     * @param string $class
33
     */
34
    public static function getNamespace(string $class): string
35
    {
36
        return substr($class, 0, strrpos($class, '\\'));
37
    }
38
39
    /**
40
     * Find class
41
     * @param string $class Class
42
     * @param string $namespace Namespace
0 ignored issues
show
Documentation introduced by
Should the type for parameter $namespace not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
43
     * @param string $rootns Root namespace
0 ignored issues
show
Documentation introduced by
Should the type for parameter $rootns not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
44
     * @return string
45
     */
46
    public static function find($class, $namespace = null, $rootns = null)
47
    {
48
        $e = explode('\\', $class);
49
        if ($e[0] === '') {
50
            return $class;
51
        }
52
        if ('Pool' === $class || 'TransportContext' === $class) {
53
            return '\\PHPDaemon\\Core\\' . $class;
54
        }
55
        if (mb_orig_strpos($class, '\\') === false && $namespace === null) {
56
            if ('Example' === substr($class, 0, 7)) {
57
                array_unshift($e, 'Examples');
58
            }
59 View Code Duplication
            if ('Server' === substr($class, -6)) {
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...
60
                $path = '\\PHPDaemon\\Servers\\' . substr($class, 0, -6) . '\\Pool';
61
                $r = str_replace('\\Servers\\Servers', '\\Servers', $path);
62
                Daemon::log('ClassFinder: \'' . $class . '\' -> \'' . $r . '\', you should change your code.');
63
                return $r;
64
            }
65 View Code Duplication
            if ('Client' === substr($class, -6)) {
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...
66
                $path = '\\PHPDaemon\\Clients\\' . substr($class, 0, -6) . '\\Pool';
67
                $r = str_replace('\\Clients\\Clients', '\\Clients', $path);
68
                Daemon::log('ClassFinder: \'' . $class . '\' -> \'' . $r . '\', you should change your code.');
69
                return $r;
70
            }
71 View Code Duplication
            if ('ClientAsync' === substr($class, -11)) {
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...
72
                $path = '\\PHPDaemon\\Clients\\' . substr($class, 0, -11) . '\\Pool';
73
                $r = str_replace('\\Client\\Clients', '\\Clients', $path);
74
                Daemon::log('ClassFinder: \'' . $class . '\' -> \'' . $r . '\', you should change your code.');
75
                return $r;
76
            }
77
        }
78
        if ($namespace !== null && sizeof($e) < 2) {
79
            array_unshift($e, $namespace);
80
        }
81
        array_unshift($e, '\\' . ($rootns !== null ? $rootns : Daemon::$config->defaultns->value));
82
        return implode('\\', $e);
83
    }
84
}
85