Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
PropertyReflection
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
9
100.00% covered (success)
100.00%
1 / 1
 parse
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Code\Reflection;
15
16use Pop\Code\Generator;
17
18/**
19 * Property reflection code class
20 *
21 * @category   Pop
22 * @package    Pop\Code
23 * @author     Nick Sagona, III <dev@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    5.0.0
27 */
28class PropertyReflection extends AbstractReflection
29{
30
31    /**
32     * Method to parse a property
33     *
34     * @param  mixed   $code
35     * @param  ?string $name
36     * @param  mixed   $value
37     * @return Generator\PropertyGenerator
38     */
39    public static function parse(mixed $code, ?string $name = null, mixed $value = null): Generator\PropertyGenerator
40    {
41        if ($code->isProtected()) {
42            $visibility = 'protected';
43        } else if ($code->isPrivate()) {
44            $visibility = 'private';
45        } else {
46            $visibility = 'public';
47        }
48
49        $docblock = null;
50        $desc     = null;
51        $type     = null;
52
53        $doc = $code->getDocComment();
54        if (($doc !== null) && (str_contains($doc, '/*'))) {
55            $docblock = DocblockReflection::parse($doc);
56            $docblock->setIndent(4);
57            $desc     = $docblock->getDesc();
58            $type     = $docblock->getTag('var');
59        } else if ($value !== null) {
60            $type     = strtolower(gettype($value));
61        }
62
63        if (is_array($value)) {
64            $formattedValue = (count($value) == 0) ? null : $value;
65        } else {
66            $formattedValue = $value;
67        }
68
69        $property = new Generator\PropertyGenerator($code->getName(), $type, $formattedValue, $visibility, $code->isStatic());
70        if ($docblock !== null) {
71            $property->setDocblock($docblock);
72        }
73        $property->setDesc($desc);
74
75        return $property;
76    }
77
78}