Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

phpDocumentor/Compiler/Pass/PackageTreeBuilder.php (4 issues)

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\Compiler\Pass;
17
18
use phpDocumentor\Compiler\CompilerPassInterface;
19
use phpDocumentor\Descriptor\Collection;
20
use phpDocumentor\Descriptor\DescriptorAbstract;
21
use phpDocumentor\Descriptor\PackageDescriptor;
22
use phpDocumentor\Descriptor\ProjectDescriptor;
23
use phpDocumentor\Descriptor\TagDescriptor;
24
25
/**
26
 * Rebuilds the package tree from the elements found in files.
27
 *
28
 * On every compiler pass is the package tree rebuild to aid in the process
29
 * of incremental updates.
30
 *
31
 * If the package tree were to be persisted then both locations needed to be
32
 * invalidated if a file were to change.
33
 */
34
class PackageTreeBuilder implements CompilerPassInterface
35
{
36
    const COMPILER_PRIORITY = 9001;
37
38
    public function getDescription(): string
39
    {
40
        return 'Build "packages" index';
41
    }
42
43
    public function execute(ProjectDescriptor $project): void
44
    {
45
        $rootPackageDescriptor = new PackageDescriptor();
46
        $rootPackageDescriptor->setName('\\');
47
        $project->getIndexes()->set('packages', new Collection());
48
        $project->getIndexes()->packages['\\'] = $rootPackageDescriptor;
49
50
        foreach ($project->getFiles() as $file) {
51
            $this->addElementsOfTypeToPackage($project, [$file], 'files');
52
            $this->addElementsOfTypeToPackage($project, $file->getConstants()->getAll(), 'constants');
53
            $this->addElementsOfTypeToPackage($project, $file->getFunctions()->getAll(), 'functions');
54
            $this->addElementsOfTypeToPackage($project, $file->getClasses()->getAll(), 'classes');
55
            $this->addElementsOfTypeToPackage($project, $file->getInterfaces()->getAll(), 'interfaces');
56
            $this->addElementsOfTypeToPackage($project, $file->getTraits()->getAll(), 'traits');
57
        }
58
    }
59
60
    /**
61
     * Adds the given elements of a specific type to their respective Package Descriptors.
62
     *
63
     * This method will assign the given elements to the package as registered in the package field of that
64
     * element. If a package does not exist yet it will automatically be created.
65
     *
66
     * @param DescriptorAbstract[] $elements Series of elements to add to their respective package.
67
     * @param string $type Declares which field of the package will be populated with the given
68
     * series of elements. This name will be transformed to a getter which must exist. Out of performance
69
     * considerations will no effort be done to verify whether the provided type is valid.
70
     */
71
    protected function addElementsOfTypeToPackage(ProjectDescriptor $project, array $elements, string $type): void
72
    {
73
        /** @var DescriptorAbstract $element */
74
        foreach ($elements as $element) {
75
            $packageName = '';
76
            $packageTags = $element->getTags()->get('package');
77
            if ($packageTags instanceof Collection) {
78
                $packageTag = $packageTags->getIterator()->current();
79
                if ($packageTag instanceof TagDescriptor) {
80
                    $packageName = $packageTag->getDescription();
81
                }
82
            }
83
84
            $subpackageCollection = $element->getTags()->get('subpackage');
85
            if ($subpackageCollection instanceof Collection && $subpackageCollection->count() > 0) {
86
                $subpackageTag = $subpackageCollection->getIterator()->current();
87
                if ($subpackageTag instanceof TagDescriptor) {
88
                    $packageName .= '\\' . $subpackageTag->getDescription();
89
                }
90
            }
91
92
            // ensure consistency by trimming the slash prefix and then re-appending it.
93
            $packageIndexName = '\\' . ltrim((string) $packageName, '\\');
94
            if (!isset($project->getIndexes()->packages[$packageIndexName])) {
0 ignored issues
show
The property packages does not exist on object<phpDocumentor\Descriptor\Collection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
95
                $this->createPackageDescriptorTree($project, (string) $packageName);
96
            }
97
98
            /** @var PackageDescriptor $package */
99
            $package = $project->getIndexes()->packages[$packageIndexName];
0 ignored issues
show
The property packages does not exist on object<phpDocumentor\Descriptor\Collection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
100
101
            // replace textual representation with an object representation
102
            $element->setPackage($package);
103
104
            // add element to package
105
            $getter = 'get' . ucfirst($type);
106
107
            /** @var Collection $collection */
108
            $collection = $package->{$getter}();
109
            $collection->add($element);
110
        }
111
    }
112
113
    /**
114
     * Creates a tree of PackageDescriptors based on the provided FQNN (package name).
115
     *
116
     * This method will examine the package name and create a package descriptor for each part of
117
     * the FQNN if it doesn't exist in the packages field of the current package (starting with the root
118
     * Package in the Project Descriptor),
119
     *
120
     * As an intended side effect this method also populates the *elements* index of the ProjectDescriptor with all
121
     * created PackageDescriptors. Each index key is prefixed with a tilde (~) so that it will not conflict with
122
     * other FQSEN's, such as classes or interfaces.
123
     *
124
     * @param string $packageName A FQNN of the package (and parents) to create.
125
     * @see ProjectDescriptor::getPackage() for the root package.
126
     * @see PackageDescriptor::getChildren() for the child packages of a given package.
127
     */
128
    protected function createPackageDescriptorTree(ProjectDescriptor $project, string $packageName): void
129
    {
130
        $parts = explode('\\', ltrim($packageName, '\\'));
131
        $fqnn = '';
132
133
        // this method does not use recursion to traverse the tree but uses a pointer that will be overridden with the
134
        // next item that is to be traversed (child package) at the end of the loop.
135
136
        /** @var PackageDescriptor $pointer */
137
        $pointer = $project->getIndexes()->packages['\\'];
0 ignored issues
show
The property packages does not exist on object<phpDocumentor\Descriptor\Collection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
138
        foreach ($parts as $part) {
139
            $fqnn .= '\\' . $part;
140
            if ($pointer->getChildren()->get($part)) {
141
                $pointer = $pointer->getChildren()->get($part);
142
                continue;
143
            }
144
145
            // package does not exist, create it
146
            $interimPackageDescriptor = new PackageDescriptor();
147
            $interimPackageDescriptor->setParent($pointer);
148
            $interimPackageDescriptor->setName($part);
149
            $interimPackageDescriptor->setFullyQualifiedStructuralElementName($fqnn);
150
151
            // add to the pointer's list of children
152
            $pointer->getChildren()->set($part ?: 'UNKNOWN', $interimPackageDescriptor);
153
154
            // add to index
155
            $project->getIndexes()->packages[$fqnn] = $interimPackageDescriptor;
0 ignored issues
show
The property packages does not exist on object<phpDocumentor\Descriptor\Collection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
156
157
            // move pointer forward
158
            $pointer = $interimPackageDescriptor;
159
        }
160
    }
161
}
162