Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
created

LegacyNamespaceConverter/LegacyNamespaceFilter.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Plugin\LegacyNamespaceConverter;
13
14
use phpDocumentor\Descriptor\DescriptorAbstract;
15
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
16
use Zend\Filter\AbstractFilter;
17
18
/**
19
 * Converts elements with underscores into a namespaced version.
20
 *
21
 * This filter will examine the Name of an element and extract namespaces based on underscores in the
22
 * name. Every underscore is treated as a namespace separator.
23
 *
24
 * @author david0 <https://github.com/david0> this plugin was generously provided by `@david0`.
25
 * @link   https://github.com/phpDocumentor/phpDocumentor2/pull/1135
26
 */
27
class LegacyNamespaceFilter extends AbstractFilter
28
{
29
    /** @var ProjectDescriptorBuilder $builder */
30
    protected $builder;
31
32
    /** @var string */
33
    private $namespacePrefix='';
34
35
    /**
36
     * Initializes this filter with an instance of the builder to retrieve the latest ProjectDescriptor from.
37
     *
38
     * @param ProjectDescriptorBuilder $builder
39
     */
40
    public function __construct(ProjectDescriptorBuilder $builder)
41
    {
42
        $this->builder = $builder;
43
    }
44
45
    /**
46
     * Overrides the name and namespace of an element with a separated version of the class name.
47
     *
48
     * If a class is separated by underscores than the last part is set as name and the first parts are set as
49
     * namespace with the namespace separator instead of an underscore.
50
     *
51
     * @param DescriptorAbstract $value
52
     *
53
     * @return DescriptorAbstract|null
54
     */
55
    public function filter($value)
56
    {
57
        if ($value) {
58
            $namespace = $value->getNamespace()=='' ? '\\' . $this->namespacePrefix : $value->getNamespace();
59
            $value->setNamespace($this->namespaceFromLegacyNamespace($namespace, $value->getName()));
0 ignored issues
show
It seems like $namespace defined by $value->getNamespace() =... $value->getNamespace() on line 58 can also be of type null or object<phpDocumentor\Des...or\NamespaceDescriptor>; however, phpDocumentor\Plugin\Leg...ceFromLegacyNamespace() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
60
            $value->setName($this->classNameFromLegacyNamespace($value->getName()));
61
        }
62
63
        return $value;
64
    }
65
66
    /**
67
     * Extracts the namespace from the class name.
68
     *
69
     * @param string $namespace
70
     * @param string $className
71
     *
72
     * @return string
73
     */
74
    private function namespaceFromLegacyNamespace($namespace, $className)
75
    {
76
        $qcn = str_replace('_', '\\', $className);
77
78
        $lastBackslash = strrpos($qcn, '\\');
79
        if ($lastBackslash) {
80
            $namespace = rtrim($namespace, '\\') . '\\' . substr($qcn, 0, $lastBackslash);
81
        }
82
83
        return $namespace;
84
    }
85
86
    /**
87
     * Extracts the class name without prefix from the full class name.
88
     *
89
     * @param string $className
90
     *
91
     * @return string
92
     */
93
    private function classNameFromLegacyNamespace($className)
94
    {
95
        $lastUnderscore = strrpos($className, '_');
96
        if ($lastUnderscore) {
97
            $className = substr($className, $lastUnderscore + 1);
98
        }
99
100
        return $className;
101
    }
102
103
    /**
104
     * Set a prefix for all elements without an namespace
105
     *
106
     * @param string $prefix
107
     */
108
    public function setNamespacePrefix($prefix)
109
    {
110
        $this->namespacePrefix = $prefix;
111
    }
112
}
113