1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpDocumentor. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright 2010-2015 Mike van Riel<[email protected]> |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
10
|
|
|
* @link http://phpdoc.org |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
namespace phpDocumentor\Reflection\Php\Factory; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use phpDocumentor\Reflection\Element; |
18
|
|
|
use phpDocumentor\Reflection\Fqsen; |
19
|
|
|
use phpDocumentor\Reflection\Php\Class_ as ClassElement; |
20
|
|
|
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy; |
21
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
22
|
|
|
use phpDocumentor\Reflection\Types\Context; |
23
|
|
|
use PhpParser\Node; |
24
|
|
|
use PhpParser\Node\Stmt\Class_ as ClassNode; |
25
|
|
|
use PhpParser\Node\Stmt\ClassConst; |
26
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
27
|
|
|
use PhpParser\Node\Stmt\Property as PropertyNode; |
28
|
|
|
use PhpParser\Comment\Doc; |
29
|
|
|
use PhpParser\Node\Stmt\TraitUse; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Strategy to create a ClassElement including all sub elements. |
33
|
|
|
*/ |
34
|
|
|
final class Class_ extends AbstractFactory implements ProjectFactoryStrategy |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Returns true when the strategy is able to handle the object. |
38
|
|
|
* |
39
|
|
|
* @param object $object object to check. |
40
|
|
|
* @return boolean |
41
|
|
|
*/ |
42
|
|
|
public function matches($object) |
43
|
1 |
|
{ |
44
|
|
|
return $object instanceof ClassNode; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Creates an ClassElement out of the given object. |
49
|
|
|
* Since an object might contain other objects that need to be converted the $factory is passed so it can be |
50
|
|
|
* used to create nested Elements. |
51
|
|
|
* |
52
|
|
|
* @param ClassNode $object object to convert to an Element |
53
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
54
|
|
|
* @param Context $context of the created object |
55
|
|
|
* @return ClassElement |
56
|
|
|
*/ |
57
|
|
|
public function doCreate($object, StrategyContainer $strategies, Context $context = null) |
58
|
9 |
|
{ |
59
|
|
|
$docBlock = $this->createDocBlock($object->getDocComment(), $strategies, $context); |
60
|
9 |
|
|
61
|
1 |
|
$classElement = new ClassElement( |
62
|
1 |
|
$object->fqsen, |
|
|
|
|
63
|
1 |
|
$docBlock, |
|
|
|
|
64
|
1 |
|
$object->extends ? new Fqsen('\\' . $object->extends) : null, |
65
|
1 |
|
$object->isAbstract(), |
66
|
1 |
|
$object->isFinal() |
67
|
|
|
); |
68
|
|
|
|
69
|
8 |
|
if (isset($object->implements)) { |
70
|
|
|
foreach ($object->implements as $interfaceClassName) { |
71
|
8 |
|
$classElement->addInterface( |
72
|
8 |
|
new Fqsen('\\' . $interfaceClassName->toString()) |
73
|
8 |
|
); |
74
|
8 |
|
} |
75
|
8 |
|
} |
76
|
8 |
|
|
77
|
8 |
|
if (isset($object->stmts)) { |
78
|
|
|
foreach ($object->stmts as $stmt) { |
79
|
8 |
|
switch (get_class($stmt)) { |
80
|
1 |
|
case TraitUse::class: |
81
|
1 |
|
foreach ($stmt->traits as $use) { |
|
|
|
|
82
|
1 |
|
$classElement->addUsedTrait(new Fqsen('\\'. $use->toString())); |
83
|
1 |
|
} |
84
|
1 |
|
break; |
85
|
1 |
|
case PropertyNode::class: |
86
|
|
|
$properties = new PropertyIterator($stmt); |
|
|
|
|
87
|
8 |
|
foreach ($properties as $property) { |
88
|
4 |
|
$element = $this->createMember($property, $strategies, $context); |
89
|
4 |
|
$classElement->addProperty($element); |
|
|
|
|
90
|
4 |
|
} |
91
|
1 |
|
break; |
92
|
1 |
|
case ClassMethod::class: |
93
|
1 |
|
$method = $this->createMember($stmt, $strategies, $context); |
94
|
1 |
|
$classElement->addMethod($method); |
|
|
|
|
95
|
3 |
|
break; |
96
|
1 |
|
case ClassConst::class: |
97
|
1 |
|
$constants = new ClassConstantIterator($stmt); |
|
|
|
|
98
|
1 |
|
foreach ($constants as $const) { |
99
|
1 |
|
$element = $this->createMember($const, $strategies, $context); |
100
|
1 |
|
$classElement->addConstant($element); |
|
|
|
|
101
|
1 |
|
} |
102
|
2 |
|
break; |
103
|
1 |
|
} |
104
|
1 |
|
} |
105
|
1 |
|
} |
106
|
1 |
|
|
107
|
1 |
|
return $classElement; |
108
|
1 |
|
} |
109
|
|
|
} |
110
|
|
|
|
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider
.