@@ -49,6 +49,9 @@ |
||
| 49 | 49 | return new static($name); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | + /** |
|
| 53 | + * @param string $name |
|
| 54 | + */ |
|
| 52 | 55 | public function __construct($name) { |
| 53 | 56 | parent::__construct($name); |
| 54 | 57 | $this->initParameters(); |
@@ -17,6 +17,9 @@ |
||
| 17 | 17 | private $visitors; |
| 18 | 18 | private $filename; |
| 19 | 19 | |
| 20 | + /** |
|
| 21 | + * @param string $filename |
|
| 22 | + */ |
|
| 20 | 23 | public function __construct($filename) { |
| 21 | 24 | $this->filename = $filename; |
| 22 | 25 | $this->visitors = new Set(); |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | * Returns whether this node is a boolean value |
| 75 | 75 | * |
| 76 | 76 | * @param Node $node |
| 77 | - * @return bool |
|
| 77 | + * @return boolean|null |
|
| 78 | 78 | */ |
| 79 | 79 | private function isBool(Node $node) { |
| 80 | 80 | if ($node instanceof ConstFetch) { |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * Returns whether this node is a null value |
| 92 | 92 | * |
| 93 | 93 | * @param Node $node |
| 94 | - * @return bool |
|
| 94 | + * @return boolean|null |
|
| 95 | 95 | */ |
| 96 | 96 | private function isNull(Node $node) { |
| 97 | 97 | if ($node instanceof ConstFetch) { |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | * Returns the value from a node |
| 105 | 105 | * |
| 106 | 106 | * @param Node $node |
| 107 | - * @return mixed |
|
| 107 | + * @return string |
|
| 108 | 108 | */ |
| 109 | 109 | private function getExpression(Node $node) { |
| 110 | 110 | if ($node instanceof ConstFetch) { |
@@ -33,10 +33,10 @@ |
||
| 33 | 33 | if ($this->isPrimitive($value)) { |
| 34 | 34 | $obj->setValue($this->getPrimitiveValue($value)); |
| 35 | 35 | } |
| 36 | - $expr = $this->getExpression($value); |
|
| 37 | - if ($expr) { |
|
| 38 | - $obj->setExpression($expr); |
|
| 39 | - } |
|
| 36 | + $expr = $this->getExpression($value); |
|
| 37 | + if ($expr) { |
|
| 38 | + $obj->setExpression($expr); |
|
| 39 | + } |
|
| 40 | 40 | } |
| 41 | 41 | } |
| 42 | 42 | |
@@ -31,7 +31,7 @@ |
||
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | if ($prevNode && $prevNode->getLine() && $node->getLine()) { |
| 34 | - $diff = $node->getLine()- $prevNode->getLine(); |
|
| 34 | + $diff = $node->getLine() - $prevNode->getLine(); |
|
| 35 | 35 | if ($diff > 0) { |
| 36 | 36 | $result .= str_repeat("\n", $diff - 1); |
| 37 | 37 | } |
@@ -9,26 +9,26 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | class PrettyPrinter extends Standard { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * Pretty prints an array of nodes (statements) and indents them optionally. |
|
| 14 | - * |
|
| 15 | - * @param Node[] $nodes Array of nodes |
|
| 16 | - * @param bool $indent Whether to indent the printed nodes |
|
| 17 | - * |
|
| 18 | - * @return string Pretty printed statements |
|
| 19 | - */ |
|
| 20 | - protected function pStmts(array $nodes, bool $indent = true): string { |
|
| 21 | - $result = ''; |
|
| 12 | + /** |
|
| 13 | + * Pretty prints an array of nodes (statements) and indents them optionally. |
|
| 14 | + * |
|
| 15 | + * @param Node[] $nodes Array of nodes |
|
| 16 | + * @param bool $indent Whether to indent the printed nodes |
|
| 17 | + * |
|
| 18 | + * @return string Pretty printed statements |
|
| 19 | + */ |
|
| 20 | + protected function pStmts(array $nodes, bool $indent = true): string { |
|
| 21 | + $result = ''; |
|
| 22 | 22 | $prevNode = null; |
| 23 | 23 | |
| 24 | - foreach ($nodes as $node) { |
|
| 25 | - $comments = $node->getAttribute('comments', []); |
|
| 26 | - if ($comments) { |
|
| 27 | - $result .= "\n" . $this->pComments($comments); |
|
| 28 | - if ($node instanceof Stmt\Nop) { |
|
| 29 | - continue; |
|
| 30 | - } |
|
| 31 | - } |
|
| 24 | + foreach ($nodes as $node) { |
|
| 25 | + $comments = $node->getAttribute('comments', []); |
|
| 26 | + if ($comments) { |
|
| 27 | + $result .= "\n" . $this->pComments($comments); |
|
| 28 | + if ($node instanceof Stmt\Nop) { |
|
| 29 | + continue; |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | 33 | if ($prevNode && $prevNode->getLine() && $node->getLine()) { |
| 34 | 34 | $diff = $node->getLine()- $prevNode->getLine(); |
@@ -37,16 +37,16 @@ discard block |
||
| 37 | 37 | } |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - $result .= "\n" . $this->p($node) . ($node instanceof Expr ? ';' : ''); |
|
| 40 | + $result .= "\n" . $this->p($node) . ($node instanceof Expr ? ';' : ''); |
|
| 41 | 41 | $prevNode = $node; |
| 42 | - } |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | 44 | if ($indent) { |
| 45 | - return preg_replace('~\n(?!$)~', "\n ", $result); |
|
| 46 | - } else { |
|
| 47 | - return $result; |
|
| 48 | - } |
|
| 49 | - } |
|
| 45 | + return preg_replace('~\n(?!$)~', "\n ", $result); |
|
| 46 | + } else { |
|
| 47 | + return $result; |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | public function pExpr_Array(Array_ $node) { |
| 52 | 52 | return '[' . $this->pCommaSeparated($node->items) . ']'; |
@@ -28,7 +28,7 @@ |
||
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * |
| 31 | - * @param AbstractModel|TypePart $model |
|
| 31 | + * @param AbstractModel $model |
|
| 32 | 32 | * @param bool $allowed |
| 33 | 33 | * |
| 34 | 34 | * @return string|null |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | 'string', 'int', 'integer', 'bool', 'boolean', 'float', 'double', 'object', 'mixed', 'resource' |
| 13 | 13 | ]; |
| 14 | 14 | |
| 15 | - protected static $php7typeHints = [ |
|
| 15 | + protected static $php7typeHints = [ |
|
| 16 | 16 | 'string', 'int', 'integer', 'bool', 'boolean', 'float', 'double' |
| 17 | 17 | ]; |
| 18 | 18 | |
@@ -36,15 +36,15 @@ discard block |
||
| 36 | 36 | private function getType(AbstractModel $model, bool $allowed, bool $nullable): ?string { |
| 37 | 37 | $types = $model->getTypes(); |
| 38 | 38 | if (!$types || $types->size() !== 1) { |
| 39 | - return null; |
|
| 40 | - } |
|
| 39 | + return null; |
|
| 40 | + } |
|
| 41 | 41 | $type = (string)$types->values()->toArray()[0]; |
| 42 | 42 | if (!in_array($type, self::$noTypeHints, true) |
| 43 | - || ($allowed && in_array($type, self::$php7typeHints, true))) { |
|
| 43 | + || ($allowed && in_array($type, self::$php7typeHints, true))) { |
|
| 44 | 44 | |
| 45 | - $type = self::$typeHintMap[$type] ?? $type; |
|
| 45 | + $type = self::$typeHintMap[$type] ?? $type; |
|
| 46 | 46 | |
| 47 | - if ($nullable && $model->getNullable()) { |
|
| 47 | + if ($nullable && $model->getNullable()) { |
|
| 48 | 48 | $type = '?' . $type; |
| 49 | 49 | } |
| 50 | 50 | |
@@ -38,7 +38,7 @@ |
||
| 38 | 38 | if (!$types || $types->size() !== 1) { |
| 39 | 39 | return null; |
| 40 | 40 | } |
| 41 | - $type = (string)$types->values()->toArray()[0]; |
|
| 41 | + $type = (string) $types->values()->toArray()[0]; |
|
| 42 | 42 | if (!in_array($type, self::$noTypeHints, true) |
| 43 | 43 | || ($allowed && in_array($type, self::$php7typeHints, true))) { |
| 44 | 44 | |
@@ -79,7 +79,6 @@ |
||
| 79 | 79 | /** |
| 80 | 80 | * Sets the parent class name |
| 81 | 81 | * |
| 82 | - * @param PhpClass|string|null $name the new parent |
|
| 83 | 82 | * @return $this |
| 84 | 83 | */ |
| 85 | 84 | public function setParentClassName($parent) { |
@@ -71,10 +71,10 @@ discard block |
||
| 71 | 71 | return $this->parentClassName; |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - public function getParentClass(): PhpClass { |
|
| 75 | - return class_exists($this->parentClassName) ? |
|
| 76 | - self::fromName($this->parentClassName) : PhpClass::create($this->parentClassName); |
|
| 77 | - } |
|
| 74 | + public function getParentClass(): PhpClass { |
|
| 75 | + return class_exists($this->parentClassName) ? |
|
| 76 | + self::fromName($this->parentClassName) : PhpClass::create($this->parentClassName); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | 79 | /** |
| 80 | 80 | * Sets the parent class name |
@@ -84,12 +84,12 @@ discard block |
||
| 84 | 84 | */ |
| 85 | 85 | public function setParentClassName($parent) { |
| 86 | 86 | if ($parent instanceof PhpClass) { |
| 87 | - $this->addUseStatement($parent->getQualifiedName()); |
|
| 88 | - $parent = $parent->getName(); |
|
| 89 | - } |
|
| 90 | - $this->parentClassName = $parent; |
|
| 87 | + $this->addUseStatement($parent->getQualifiedName()); |
|
| 88 | + $parent = $parent->getName(); |
|
| 89 | + } |
|
| 90 | + $this->parentClassName = $parent; |
|
| 91 | 91 | |
| 92 | - return $this; |
|
| 92 | + return $this; |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | public function generateDocblock(): void { |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | * A quick way to add a parameter which is created from the given parameters |
| 36 | 36 | * |
| 37 | 37 | * @param string $name |
| 38 | - * @param string[]|PhpTypeInterface[] $types |
|
| 38 | + * @param string[]|PhpTypeInterface[] $type |
|
| 39 | 39 | * @param mixed $defaultValue omit the argument to define no default value |
| 40 | 40 | * |
| 41 | 41 | * @return $this |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * A quick way to add a parameter with description which is created from the given parameters |
| 47 | 47 | * |
| 48 | 48 | * @param string $name |
| 49 | - * @param string[]|PhpTypeInterface[] $types |
|
| 49 | + * @param string[]|PhpTypeInterface[] $type |
|
| 50 | 50 | * @param null|string $typeDescription |
| 51 | 51 | * @param mixed $defaultValue omit the argument to define no default value |
| 52 | 52 | * |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | /** |
| 85 | 85 | * Returns a collection of parameters |
| 86 | 86 | * |
| 87 | - * @return array |
|
| 87 | + * @return PhpParameter[] |
|
| 88 | 88 | */ |
| 89 | 89 | public function getParameters(); |
| 90 | 90 | |
@@ -7,75 +7,75 @@ |
||
| 7 | 7 | |
| 8 | 8 | class TypeUtils |
| 9 | 9 | { |
| 10 | - public static $phpTypes = [ |
|
| 11 | - 'string', |
|
| 12 | - 'int', |
|
| 13 | - 'integer', |
|
| 14 | - 'bool', |
|
| 15 | - 'boolean', |
|
| 16 | - 'float', |
|
| 17 | - 'double', |
|
| 18 | - 'object', |
|
| 19 | - 'mixed', |
|
| 20 | - 'resource', |
|
| 21 | - 'iterable', |
|
| 22 | - 'array', |
|
| 23 | - 'callable', |
|
| 24 | - ]; |
|
| 10 | + public static $phpTypes = [ |
|
| 11 | + 'string', |
|
| 12 | + 'int', |
|
| 13 | + 'integer', |
|
| 14 | + 'bool', |
|
| 15 | + 'boolean', |
|
| 16 | + 'float', |
|
| 17 | + 'double', |
|
| 18 | + 'object', |
|
| 19 | + 'mixed', |
|
| 20 | + 'resource', |
|
| 21 | + 'iterable', |
|
| 22 | + 'array', |
|
| 23 | + 'callable', |
|
| 24 | + ]; |
|
| 25 | 25 | |
| 26 | - public static function expressionToTypes(?string $typeExpression): array { |
|
| 27 | - if (!$typeExpression) { |
|
| 28 | - return []; |
|
| 29 | - } |
|
| 26 | + public static function expressionToTypes(?string $typeExpression): array { |
|
| 27 | + if (!$typeExpression) { |
|
| 28 | + return []; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - return explode('|', $typeExpression); |
|
| 32 | - } |
|
| 31 | + return explode('|', $typeExpression); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - public static function guessQualifiedName(AbstractPhpStruct $stuct, string $type): string { |
|
| 35 | - if (in_array($type, self::$phpTypes, true)) { |
|
| 36 | - return $type; |
|
| 37 | - } |
|
| 34 | + public static function guessQualifiedName(AbstractPhpStruct $stuct, string $type): string { |
|
| 35 | + if (in_array($type, self::$phpTypes, true)) { |
|
| 36 | + return $type; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - $suffix = ''; |
|
| 40 | - if (strpos($type, '[]') !== false) { |
|
| 41 | - $type = str_replace('[]', '', $type); |
|
| 42 | - $suffix = '[]'; |
|
| 43 | - } |
|
| 39 | + $suffix = ''; |
|
| 40 | + if (strpos($type, '[]') !== false) { |
|
| 41 | + $type = str_replace('[]', '', $type); |
|
| 42 | + $suffix = '[]'; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - $uses = $stuct->getUseStatements(); |
|
| 46 | - foreach ($uses as $use) { |
|
| 47 | - $regexp = '/\\\\' . preg_quote($type, '/') . '$/'; |
|
| 48 | - if (preg_match($regexp, $use)) { |
|
| 49 | - return $use . $suffix; |
|
| 50 | - } |
|
| 51 | - } |
|
| 45 | + $uses = $stuct->getUseStatements(); |
|
| 46 | + foreach ($uses as $use) { |
|
| 47 | + $regexp = '/\\\\' . preg_quote($type, '/') . '$/'; |
|
| 48 | + if (preg_match($regexp, $use)) { |
|
| 49 | + return $use . $suffix; |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - $sameNamespace = $stuct->getNamespace() . '\\' . $type; |
|
| 54 | - if (class_exists($sameNamespace)) { |
|
| 55 | - return $sameNamespace . $suffix; |
|
| 56 | - } |
|
| 53 | + $sameNamespace = $stuct->getNamespace() . '\\' . $type; |
|
| 54 | + if (class_exists($sameNamespace)) { |
|
| 55 | + return $sameNamespace . $suffix; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - return $type . $suffix; |
|
| 59 | - } |
|
| 58 | + return $type . $suffix; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - public static function isGlobalQualifiedName(string $name): bool { |
|
| 62 | - return $name[0] === '\\' && substr_count($name, '\\') === 1; |
|
| 63 | - } |
|
| 61 | + public static function isGlobalQualifiedName(string $name): bool { |
|
| 62 | + return $name[0] === '\\' && substr_count($name, '\\') === 1; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - public static function isNativeType(string $type): bool { |
|
| 66 | - return in_array($type, self::$phpTypes, true); |
|
| 67 | - } |
|
| 65 | + public static function isNativeType(string $type): bool { |
|
| 66 | + return in_array($type, self::$phpTypes, true); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - public static function typesToExpression(iterable $types): ?string { |
|
| 70 | - $typeExpr = ''; |
|
| 71 | - foreach ($types as $type) { |
|
| 72 | - $typeExpr .= '|' . $type; |
|
| 73 | - } |
|
| 69 | + public static function typesToExpression(iterable $types): ?string { |
|
| 70 | + $typeExpr = ''; |
|
| 71 | + foreach ($types as $type) { |
|
| 72 | + $typeExpr .= '|' . $type; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - if (!$typeExpr) { |
|
| 76 | - return null; |
|
| 77 | - } |
|
| 75 | + if (!$typeExpr) { |
|
| 76 | + return null; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - return trim($typeExpr, '|'); |
|
| 80 | - } |
|
| 79 | + return trim($typeExpr, '|'); |
|
| 80 | + } |
|
| 81 | 81 | } |
@@ -5,8 +5,7 @@ |
||
| 5 | 5 | |
| 6 | 6 | use gossi\codegen\model\AbstractPhpStruct; |
| 7 | 7 | |
| 8 | -class TypeUtils |
|
| 9 | -{ |
|
| 8 | +class TypeUtils { |
|
| 10 | 9 | public static $phpTypes = [ |
| 11 | 10 | 'string', |
| 12 | 11 | 'int', |
@@ -57,12 +57,12 @@ discard block |
||
| 57 | 57 | return new static($name); |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - public static function fromName(string $name): self |
|
| 61 | - { |
|
| 62 | - $ref = new \ReflectionClass($name); |
|
| 60 | + public static function fromName(string $name): self |
|
| 61 | + { |
|
| 62 | + $ref = new \ReflectionClass($name); |
|
| 63 | 63 | |
| 64 | - return static::fromFile($ref->getFileName()); |
|
| 65 | - } |
|
| 64 | + return static::fromFile($ref->getFileName()); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | 67 | /** |
| 68 | 68 | * Creates a new struct |
@@ -135,17 +135,17 @@ discard block |
||
| 135 | 135 | * @return $this |
| 136 | 136 | */ |
| 137 | 137 | public function addUseStatement($qualifiedName, string $alias = null) { |
| 138 | - if ($qualifiedName instanceof PhpTypeInterface) { |
|
| 139 | - $qualifiedName = $qualifiedName->getQualifiedName(); |
|
| 140 | - } |
|
| 138 | + if ($qualifiedName instanceof PhpTypeInterface) { |
|
| 139 | + $qualifiedName = $qualifiedName->getQualifiedName(); |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - if (TypeUtils::isGlobalQualifiedName($qualifiedName) || TypeUtils::isNativeType($qualifiedName)) { |
|
| 143 | - return $this; |
|
| 144 | - } |
|
| 142 | + if (TypeUtils::isGlobalQualifiedName($qualifiedName) || TypeUtils::isNativeType($qualifiedName)) { |
|
| 143 | + return $this; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - if ($qualifiedName === $this->getNamespace()) { |
|
| 147 | - return $this; |
|
| 148 | - } |
|
| 146 | + if ($qualifiedName === $this->getNamespace()) { |
|
| 147 | + return $this; |
|
| 148 | + } |
|
| 149 | 149 | if (!is_string($alias)) { |
| 150 | 150 | if (false === strpos($qualifiedName, '\\')) { |
| 151 | 151 | $alias = $qualifiedName; |
@@ -271,24 +271,24 @@ discard block |
||
| 271 | 271 | $method->setParent($this); |
| 272 | 272 | $this->methods->set($method->getName(), $method); |
| 273 | 273 | $types = $method->getTypes(); |
| 274 | - if ($types) { |
|
| 275 | - foreach ($types as $type) { |
|
| 276 | - $this->addUseStatement($type); |
|
| 277 | - $method->addType($type); |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - foreach ($method->getParameters() as $parameter) { |
|
| 282 | - $types = $parameter->getTypes(); |
|
| 283 | - if ($types) { |
|
| 284 | - foreach ($types as $type) { |
|
| 285 | - $this->addUseStatement($type); |
|
| 286 | - $parameter->addType($type); |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - return $this; |
|
| 274 | + if ($types) { |
|
| 275 | + foreach ($types as $type) { |
|
| 276 | + $this->addUseStatement($type); |
|
| 277 | + $method->addType($type); |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + foreach ($method->getParameters() as $parameter) { |
|
| 282 | + $types = $parameter->getTypes(); |
|
| 283 | + if ($types) { |
|
| 284 | + foreach ($types as $type) { |
|
| 285 | + $this->addUseStatement($type); |
|
| 286 | + $parameter->addType($type); |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + return $this; |
|
| 292 | 292 | } |
| 293 | 293 | |
| 294 | 294 | /** |