Skip to content

Commit b964e0a

Browse files
committed
Merge branch 'feature/issue-11' into develop
2 parents 6b3bb30 + 3975f68 commit b964e0a

35 files changed

+418
-309
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
# CHANGELOG
22

3+
## 4.0.0 - 2021/02/xx
4+
- issue #11 - Allow class property to be typed
5+
- Clear, refactor and review code
6+
- Introduce `AccessRestrictedElementInterface`, `AssignedValueElementInterface` and `TypeHintedElementInterface`
7+
- Introduce `AccessRestrictedElementTrait`, `AssignedValueElementTrait` and `TypeHintedElementTrait`
8+
- Version 3.0 is no more maintained
9+
310
## 3.0.2 - 2021/01/28
411
- Update Travis CI badge and settings
512

613
## 3.0.1 - 2021/01/26
714
- Fix typo
815

916
## 3.0.0 - 2021/01/26
10-
- use `splitbrain/phpfarm:jessie` as Docker image and fix docker image settings
17+
- Use `splitbrain/phpfarm:jessie` as Docker image and fix docker image settings
1118
- Code requires PHP >= 7.4
1219
- Code cleaning
1320
- BC:

UPGRADE-4.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# UPGRADE FROM 3.* to 4.*
2+
3+
The main change is that `PhpProperty` are now type hinted in addition to internal methods removal and introduction of Interfaces and Traits
4+
- `WsdlToPhp\PhpGenerator\Element\PhpProperty::__construct` has a new parameter in the end named `$type` which can be any of the regular PHP types, or any existing PHP class or a PhpClass element or a string or a null value
5+
6+
Apart from that, the usage did not change, the inner classes has changed which should not be an issue if you did not inherit from them. Otherwise, launch your unit tests ;).

src/Component/AbstractComponent.php

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,9 @@ public function __toString(): string
2525

2626
public function toString(): string
2727
{
28-
$content = [];
29-
foreach ($this->getElements() as $element) {
30-
$content[] = $this->getElementString($element);
31-
}
32-
33-
return implode('', $content);
28+
return (string) $this->mainElement;
3429
}
3530

36-
/**
37-
* @return AbstractElement[]|string[]
38-
*/
39-
abstract public function getElements(): array;
40-
4131
public function setMainElement(AbstractElement $element): self
4232
{
4333
if ($element instanceof PhpFileElement || $element instanceof PhpClassElement) {
@@ -49,14 +39,6 @@ public function setMainElement(AbstractElement $element): self
4939
return $this;
5040
}
5141

52-
/**
53-
* @return PhpClassElement|PhpFileElement
54-
*/
55-
public function getMainElement(): AbstractElement
56-
{
57-
return $this->mainElement;
58-
}
59-
6042
public function addConstantElement(PhpConstantElement $constant): self
6143
{
6244
if (!$constant->getClass() instanceof PhpClassElement && $this->mainElement instanceof PhpClassElement) {
@@ -69,8 +51,6 @@ public function addConstantElement(PhpConstantElement $constant): self
6951

7052
/**
7153
* @param mixed $value
72-
*
73-
* @return AbstractComponent
7454
*/
7555
public function addConstant(string $name, $value = null, ?PhpClassElement $class = null): self
7656
{
@@ -86,8 +66,6 @@ public function addAnnotationBlockElement(PhpAnnotationBlockElement $annotationB
8666

8767
/**
8868
* @param array|PhpAnnotationBlockElement|string $annotations
89-
*
90-
* @return AbstractComponent
9169
*/
9270
public function addAnnotationBlock($annotations): self
9371
{
@@ -96,20 +74,10 @@ public function addAnnotationBlock($annotations): self
9674
]));
9775
}
9876

99-
/**
100-
* @param AbstractElement|string $element
101-
*
102-
* @throws InvalidArgumentException
103-
*/
104-
protected function getElementString($element): string
77+
public function addString(string $string = ''): self
10578
{
106-
$string = '';
107-
if (is_scalar($element)) {
108-
$string = $element;
109-
} elseif ($element instanceof AbstractElement) {
110-
$string = $element->toString();
111-
}
79+
$this->mainElement->addChild($string);
11280

113-
return $string;
81+
return $this;
11482
}
11583
}

src/Component/PhpClass.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,27 @@ public function __construct(string $name, bool $abstract = false, ?string $exten
1515
$this->setMainElement(new PhpClassElement($name, $abstract, $extends, $interfaces));
1616
}
1717

18-
public function addMethodElement(PhpMethodElement $method): AbstractComponent
18+
public function addMethodElement(PhpMethodElement $method): self
1919
{
2020
$this->mainElement->addChild($method);
2121

2222
return $this;
2323
}
2424

25-
public function addMethod(string $name, array $parameters = [], ?string $returnType = null, string $access = PhpMethodElement::ACCESS_PUBLIC, bool $abstract = false, bool $static = false, bool $final = false, bool $hasBody = true): AbstractComponent
25+
public function addMethod(string $name, array $parameters = [], ?string $returnType = null, string $access = PhpMethodElement::ACCESS_PUBLIC, bool $abstract = false, bool $static = false, bool $final = false, bool $hasBody = true): self
2626
{
2727
return $this->addMethodElement(new PhpMethodElement($name, $parameters, $returnType, $access, $abstract, $static, $final, $hasBody));
2828
}
2929

30-
public function addPropertyElement(PhpPropertyElement $property): AbstractComponent
30+
public function addPropertyElement(PhpPropertyElement $property): self
3131
{
3232
$this->mainElement->addChild($property);
3333

3434
return $this;
3535
}
3636

37-
public function addProperty(string $name, $value = null, string $access = PhpPropertyElement::ACCESS_PUBLIC): AbstractComponent
37+
public function addProperty(string $name, $value = null, string $access = PhpPropertyElement::ACCESS_PUBLIC, $type = null): self
3838
{
39-
return $this->addPropertyElement(new PhpPropertyElement($name, $value, $access));
40-
}
41-
42-
public function getElements(): array
43-
{
44-
return [
45-
$this->getMainElement(),
46-
];
39+
return $this->addPropertyElement(new PhpPropertyElement($name, $value, $access, $type));
4740
}
4841
}

src/Component/PhpFile.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ public function __construct(string $name)
1818
$this->setMainElement(new PhpFileElement($name));
1919
}
2020

21-
/**
22-
* @return PhpFileElement[]
23-
*/
24-
public function getElements(): array
25-
{
26-
return [
27-
$this->getMainElement(),
28-
];
29-
}
30-
3121
public function addClassComponent(PhpClassComponent $class): self
3222
{
3323
$this->mainElement->addChild($class->toString());

src/Component/PhpInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(string $name, bool $abstract = false, ?string $exten
1515
$this->setMainElement(new PhpInterfaceElement($name, $abstract, $extends, $interfaces));
1616
}
1717

18-
public function addMethodElement(PhpMethodElement $method): AbstractComponent
18+
public function addMethodElement(PhpMethodElement $method): self
1919
{
2020
if ($method->getHasBody()) {
2121
$method->setHasBody(false);
@@ -24,17 +24,17 @@ public function addMethodElement(PhpMethodElement $method): AbstractComponent
2424
return parent::addMethodElement($method);
2525
}
2626

27-
public function addMethod(string $name, array $parameters = [], ?string $returnType = null, string $access = PhpMethodElement::ACCESS_PUBLIC, bool $abstract = false, bool $static = false, bool $final = false, bool $hasBody = true): AbstractComponent
27+
public function addMethod(string $name, array $parameters = [], ?string $returnType = null, string $access = PhpMethodElement::ACCESS_PUBLIC, bool $abstract = false, bool $static = false, bool $final = false, bool $hasBody = true): self
2828
{
2929
return $this->addMethodElement(new PhpMethodElement($name, $parameters, $returnType, $access, $abstract, $static, $final, false));
3030
}
3131

32-
public function addPropertyElement(PhpPropertyElement $property): AbstractComponent
32+
public function addPropertyElement(PhpPropertyElement $property): self
3333
{
3434
return $this;
3535
}
3636

37-
public function addProperty(string $name, $value = null, string $access = PhpPropertyElement::ACCESS_PUBLIC): AbstractComponent
37+
public function addProperty(string $name, $value = null, string $access = PhpPropertyElement::ACCESS_PUBLIC, $type = null): self
3838
{
3939
return $this;
4040
}

src/Element/AbstractAccessRestrictedElement.php

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/Element/AbstractElement.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ abstract class AbstractElement implements GenerateableInterface
1313
/**
1414
* @var AbstractElement[]|mixed[]
1515
*/
16-
protected array $children;
16+
protected array $children = [];
1717

18-
protected int $indentation;
18+
protected int $indentation = 0;
1919

2020
public function __construct(string $name)
2121
{
2222
$this->setName($name);
23-
$this->children = [];
24-
$this->indentation = 0;
2523
}
2624

2725
public function __toString(): string
@@ -47,7 +45,7 @@ public function getName(): string
4745
public static function nameIsValid(string $name, bool $allowBackslash = false): bool
4846
{
4947
$pattern = '/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/';
50-
if (true === $allowBackslash) {
48+
if ($allowBackslash) {
5149
$pattern = '/[a-zA-Z_\x7f-\xff\\\][a-zA-Z0-9_\x7f-\xff\\\]*/';
5250
}
5351

@@ -61,7 +59,7 @@ public static function stringIsValid($string, bool $checkName = true, bool $allo
6159

6260
public static function objectIsValid($object, ?string $checkClass = null): bool
6361
{
64-
return is_object($object) && (null === $checkClass || get_class($object) === $checkClass);
62+
return is_object($object) && (is_null($checkClass) || get_class($object) === $checkClass);
6563
}
6664

6765
public function toString(?int $indentation = null): string
@@ -78,7 +76,7 @@ public function toString(?int $indentation = null): string
7876
}
7977
$lines[] = $this->getToStringAfterChildren($indentation);
8078

81-
return implode(self::BREAK_LINE_CHAR, self::cleanArrayToString($lines));
79+
return implode(self::BREAK_LINE_CHAR, static::cleanArrayToString($lines));
8280
}
8381

8482
public function getPhpName(): string
@@ -152,7 +150,7 @@ public function useBracketsForChildren(): bool
152150
public function getBracketBeforeChildren(?int $indentation = null): string
153151
{
154152
$line = $this->getIndentedString(self::OPEN_BRACKET, $indentation);
155-
$this->setIndentation((null === $indentation ? $this->getIndentation() : $indentation) + 1);
153+
$this->setIndentation((is_null($indentation) ? $this->getIndentation() : $indentation) + 1);
156154

157155
return $line;
158156
}
@@ -162,7 +160,7 @@ public function getBracketBeforeChildren(?int $indentation = null): string
162160
*/
163161
public function getBracketAfterChildren(?int $indentation = null): string
164162
{
165-
$this->setIndentation((null === $indentation ? $this->getIndentation() : $indentation) - 1);
163+
$this->setIndentation((is_null($indentation) ? $this->getIndentation() : $indentation) - 1);
166164

167165
return $this->getIndentedString(self::CLOSE_BRACKET, $indentation);
168166
}
@@ -181,7 +179,7 @@ public function getIndentation(): int
181179

182180
public function getIndentationString(?int $indentation = null): string
183181
{
184-
return str_repeat(self::INDENTATION_CHAR, null === $indentation ? $this->getIndentation() : $indentation);
182+
return str_repeat(self::INDENTATION_CHAR, is_null($indentation) ? $this->getIndentation() : $indentation);
185183
}
186184

187185
public function getIndentedString(string $string, ?int $indentation = null): string
@@ -205,7 +203,7 @@ protected function getChildContent($child, int $indentation = null): string
205203
if (is_string($child)) {
206204
$content = $this->getIndentedString($child, $indentation);
207205
} elseif ($child instanceof AbstractElement) {
208-
$content = $child->toString(null === $indentation ? $this->getIndentation() : $indentation);
206+
$content = $child->toString(is_null($indentation) ? $this->getIndentation() : $indentation);
209207
}
210208

211209
return $content;
@@ -258,9 +256,11 @@ private static function cleanArrayToString(array $array): array
258256
{
259257
$newArray = [];
260258
foreach ($array as $line) {
261-
if (null !== $line) {
262-
$newArray[] = $line;
259+
if (is_null($line)) {
260+
continue;
263261
}
262+
263+
$newArray[] = $line;
264264
}
265265

266266
return $newArray;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WsdlToPhp\PhpGenerator\Element;
6+
7+
interface AccessRestrictedElementInterface
8+
{
9+
const ACCESS_PRIVATE = 'private';
10+
11+
const ACCESS_PROTECTED = 'protected';
12+
13+
const ACCESS_PUBLIC = 'public';
14+
15+
const ACCESSES = [
16+
self::ACCESS_PRIVATE,
17+
self::ACCESS_PROTECTED,
18+
self::ACCESS_PUBLIC,
19+
];
20+
21+
public function setAccess(?string $access): AbstractElement;
22+
23+
public function getAccess(): string;
24+
25+
public static function accessIsValid(?string $access): bool;
26+
}

0 commit comments

Comments
 (0)