Completed
Push — develop ( 80740b...61b5c3 )
by Mike
10:20
created

LegacyNamespaceConverter/LegacyNamespaceFilter.php (1 issue)

call_checks.maybe_mismatching_type_passed_with_def

Bug Minor

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
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Plugin\LegacyNamespaceConverter;
17
18
use phpDocumentor\Descriptor\DescriptorAbstract;
19
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
20
use Zend\Filter\AbstractFilter;
21
22
/**
23
 * Converts elements with underscores into a namespaced version.
24
 *
25
 * This filter will examine the Name of an element and extract namespaces based on underscores in the
26
 * name. Every underscore is treated as a namespace separator.
27
 *
28
 * @author david0 <https://github.com/david0> this plugin was generously provided by `@david0`.
29
 * @link   https://github.com/phpDocumentor/phpDocumentor2/pull/1135
30
 */
31
class LegacyNamespaceFilter extends AbstractFilter
32
{
33
    /** @var ProjectDescriptorBuilder $builder */
34
    protected $builder;
35
36
    /** @var string */
37
    private $namespacePrefix = '';
38
39
    /**
40
     * Initializes this filter with an instance of the builder to retrieve the latest ProjectDescriptor from.
41
     */
42
    public function __construct(ProjectDescriptorBuilder $builder)
43
    {
44
        $this->builder = $builder;
45
    }
46
47
    /**
48
     * Overrides the name and namespace of an element with a separated version of the class name.
49
     *
50
     * If a class is separated by underscores than the last part is set as name and the first parts are set as
51
     * namespace with the namespace separator instead of an underscore.
52
     *
53
     * @param DescriptorAbstract $value
54
     *
55
     * @return DescriptorAbstract|null
56
     */
57 7
    public function filter($value)
58
    {
59 7
        if ($value) {
60 7
            $namespace = $value->getNamespace() === '' ? '\\' . $this->namespacePrefix : $value->getNamespace();
61 7
            $value->setNamespace($this->namespaceFromLegacyNamespace($namespace, $value->getName()));
0 ignored issues
show
It seems like $namespace defined by $value->getNamespace() =... $value->getNamespace() on line 60 can also be of type 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...
62 7
            $value->setName($this->classNameFromLegacyNamespace($value->getName()));
63
        }
64
65 7
        return $value;
66
    }
67
68
    /**
69
     * Extracts the namespace from the class name.
70
     *
71
     * @param string $namespace
72
     * @param string $className
73
     *
74
     * @return string
75
     */
76
    private function namespaceFromLegacyNamespace($namespace, $className)
77
    {
78
        $qcn = str_replace('_', '\\', $className);
79
80
        $lastBackslash = strrpos($qcn, '\\');
81
        if ($lastBackslash) {
82
            $namespace = rtrim($namespace, '\\') . '\\' . substr($qcn, 0, $lastBackslash);
83
        }
84
85
        return $namespace;
86
    }
87
88
    /**
89
     * Extracts the class name without prefix from the full class name.
90
     *
91
     * @param string $className
92
     *
93
     * @return string
94
     */
95
    private function classNameFromLegacyNamespace($className)
96
    {
97
        $lastUnderscore = strrpos($className, '_');
98
        if ($lastUnderscore) {
99
            $className = substr($className, $lastUnderscore + 1);
100
        }
101
102
        return $className;
103
    }
104
105
    /**
106
     * Set a prefix for all elements without an namespace
107
     *
108
     * @param string $prefix
109
     */
110
    public function setNamespacePrefix($prefix)
111
    {
112
        $this->namespacePrefix = $prefix;
113
    }
114
}
115