Completed
Pull Request — master (#68)
by Cristiano
05:36
created

PhpClass   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 12
dl 0
loc 76
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the php-code-generator package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @license Apache-2.0
8
 */
9
10
namespace gossi\codegen\model;
11
12
use gossi\codegen\model\parts\AbstractPart;
13
use gossi\codegen\model\parts\ConstantsPart;
14
use gossi\codegen\model\parts\FinalPart;
15
use gossi\codegen\model\parts\InterfacesPart;
16
use gossi\codegen\model\parts\PropertiesPart;
17
use gossi\codegen\model\parts\TraitsPart;
18
use gossi\codegen\parser\FileParser;
19
use gossi\codegen\parser\visitor\ClassParserVisitor;
20
use gossi\codegen\parser\visitor\ConstantParserVisitor;
21
use gossi\codegen\parser\visitor\MethodParserVisitor;
22
use gossi\codegen\parser\visitor\PropertyParserVisitor;
23
use phootwork\file\exception\FileException;
24
25
/**
26
 * Represents a PHP class.
27
 *
28
 * @author Thomas Gossmann
29
 */
30
class PhpClass extends AbstractPhpStruct implements GenerateableInterface, TraitsInterface, ConstantsInterface, PropertiesInterface {
31
	use AbstractPart;
32
	use ConstantsPart;
33
	use FinalPart;
34
	use InterfacesPart;
35
	use PropertiesPart;
36
	use TraitsPart;
37
38
	/** @var string */
39
	private string $parentClassName = '';
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
40
41 12
	/**
42 12
	 * Creates a PHP class from file
43 12
	 *
44 12
	 * @param string $filename
45 12
	 *
46 12
	 * @throws FileException
47 12
	 *
48 12
	 * @return PhpClass
49
	 */
50 12
	public static function fromFile(string $filename): self {
51
		$class = new self();
52
		$parser = new FileParser($filename);
53
		$parser->addVisitor(new ClassParserVisitor($class));
54
		$parser->addVisitor(new MethodParserVisitor($class));
55
		$parser->addVisitor(new ConstantParserVisitor($class));
56
		$parser->addVisitor(new PropertyParserVisitor($class));
57
		$parser->parse();
58 46
59 46
		return $class;
60 46
	}
61 46
62 46
	/**
63 46
	 * Creates a new PHP class
64
	 *
65
	 * @param string $name the qualified name
66
	 */
67
	public function __construct(string $name = '') {
68
		parent::__construct($name);
69
		$this->initProperties();
70 16
		$this->initConstants();
71 16
		$this->initInterfaces();
72
		$this->initTraits();
73
	}
74
75
	/**
76
	 * Returns the parent class name
77
	 *
78
	 * @return string
79
	 */
80 6
	public function getParentClassName(): string {
81 6
		return $this->parentClassName;
82
	}
83 6
84
	/**
85
	 * Sets the parent class name
86 5
	 *
87 5
	 * @param string $name the new parent
88
	 *
89 5
	 * @return $this
90 2
	 */
91
	public function setParentClassName(string $name): self {
92
		$this->parentClassName = $name;
93 5
94 3
		return $this;
95
	}
96 5
97
	public function hasParent(): bool {
98
		return $this->parentClassName !== '';
99
	}
100
101
	public function generateDocblock(): void {
102
		parent::generateDocblock();
103
104
		$this->constants->each(fn (string $key, PhpConstant $constant) => $constant->generateDocblock());
105
		$this->properties->each(fn (string $key, PhpProperty $property) => $property->generateDocblock());
106
	}
107
}
108