Skip to content

Commit 5ebbe4e

Browse files
author
Pavel
committed
master: Some fixes in SMD generator
1 parent b1e824e commit 5ebbe4e

File tree

8 files changed

+189
-97
lines changed

8 files changed

+189
-97
lines changed

src/DocBlock/ApiEnum.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
77
use phpDocumentor\Reflection\DocBlock\Tags\BaseTag;
88
use phpDocumentor\Reflection\DocBlock\Tags\Factory\StaticMethod;
9-
use phpDocumentor\Reflection\Type;
109
use phpDocumentor\Reflection\TypeResolver;
1110
use phpDocumentor\Reflection\Types\Context as TypeContext;
1211
use Webmozart\Assert\Assert;
@@ -27,11 +26,11 @@ class ApiEnum extends BaseTag implements StaticMethod
2726
protected $typeName = '';
2827

2928
/** @var mixed */
30-
protected $value = null;
29+
protected $value;
3130

3231
/**
33-
* @param string $variableName
34-
* @param Type $type
32+
* @param string $typeName
33+
* @param mixed $value
3534
* @param Description $description
3635
*/
3736
public function __construct($typeName, $value, Description $description = null)
@@ -58,7 +57,12 @@ public static function create(
5857

5958
preg_match(self::REGEXP, $body, $parts);
6059

61-
$description = $descriptionFactory->create(trim($parts['description']), $context);
60+
$description = null;
61+
62+
if (null !== $descriptionFactory) {
63+
$descriptionStr = isset($parts['description']) ? trim($parts['description']) : '';
64+
$description = $descriptionFactory->create($descriptionStr, $context);
65+
}
6266

6367
/** @var static $param */
6468
return new static($parts['typeName'], self::getRealValue($parts['value']), $description);

src/DocBlock/ApiObject.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
namespace Tochka\JsonRpc\DocBlock;
44

5-
use phpDocumentor\Reflection\DocBlock\Description;
65
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
7-
use phpDocumentor\Reflection\DocBlock\Tags\BaseTag;
8-
use phpDocumentor\Reflection\DocBlock\Tags\Factory\StaticMethod;
9-
use phpDocumentor\Reflection\Type;
106
use phpDocumentor\Reflection\TypeResolver;
117
use phpDocumentor\Reflection\Types\Context as TypeContext;
128
use Webmozart\Assert\Assert;

src/DocBlock/ApiParam.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use phpDocumentor\Reflection\DocBlock\Tags\Factory\StaticMethod;
99
use phpDocumentor\Reflection\Type;
1010
use phpDocumentor\Reflection\TypeResolver;
11-
use Tochka\JsonRpc\DocBlock\TypeResolver as CustomTypeResolver;
1211
use phpDocumentor\Reflection\Types\Context as TypeContext;
12+
use Tochka\JsonRpc\DocBlock\TypeResolver as CustomTypeResolver;
1313
use Webmozart\Assert\Assert;
1414

1515
/**
@@ -19,7 +19,7 @@ class ApiParam extends BaseTag implements StaticMethod
1919
{
2020
use VariableValueTrait;
2121

22-
const REGEXP = '/((?<require>\*) +)?(((?<type>[a-z\[\]]+)(\=(?<typeFormat>[a-z0-9]+|"[^"]+"|\([^\)]+\)))?) +)(\$(?<variableName>[a-z\._0-9\[\]]+)(=(?<defaultValue>[a-z0-9\.\-]+|\"[^\"]+\"))?[ \n]+)(\((?<exampleValue>[a-z0-9\.\-]+|\"[^\"]+\")\)[ \n]+)?(?<description>.*)?/is';
22+
const REGEXP = '/((?<require>\*) +)?(((?<type>[a-z\[\]]+)(\=(?<typeFormat>[a-z0-9]+|"[^"]+"|\([^\)]+\)))?) +)(\$(?<variableName>[a-z\._0-9\[\]]+)(=(?<defaultValue>[a-z0-9\.\-]+|\"[^\"]*\"))?[ \n]+)(\((?<exampleValue>[a-z0-9\.\-]+|\"[^\"]+\")\)[ \n]+)?(?<description>.*)?/is';
2323

2424
/** @var string */
2525
protected $name = 'apiParam';
@@ -46,8 +46,8 @@ class ApiParam extends BaseTag implements StaticMethod
4646
protected $optional = true;
4747

4848
/**
49-
* @param string $variableName
50-
* @param Type $type
49+
* @param string $variableName
50+
* @param Type $type
5151
* @param Description $description
5252
*/
5353
public function __construct($variableName, Type $type = null, Description $description = null)
@@ -60,6 +60,11 @@ public function __construct($variableName, Type $type = null, Description $descr
6060
}
6161

6262
/**
63+
* @param $body
64+
* @param TypeResolver|null $typeResolver
65+
* @param DescriptionFactory|null $descriptionFactory
66+
* @param TypeContext|null $context
67+
*
6368
* @return static
6469
*/
6570
public static function create(
@@ -74,12 +79,21 @@ public static function create(
7479

7580
preg_match(self::REGEXP, $body, $parts);
7681

77-
$descriptionStr = isset($parts['description']) ? trim($parts['description']) : '';
78-
$description = $descriptionFactory->create($descriptionStr, $context);
79-
$type = CustomTypeResolver::resolve(trim($parts['type']), $parts['typeFormat']);
82+
$description = null;
83+
84+
if (null !== $descriptionFactory) {
85+
$descriptionStr = isset($parts['description']) ? trim($parts['description']) : '';
86+
$description = $descriptionFactory->create($descriptionStr, $context);
87+
}
88+
89+
$typeStr = isset($parts['type']) ? trim($parts['type']) : 'mixed';
90+
$typeExtended = isset($parts['typeFormat']) ? $parts['typeFormat'] : null;
91+
$variableName = isset($parts['variableName']) ? $parts['variableName'] : 'variable';
92+
93+
$type = CustomTypeResolver::resolve($typeStr, $typeExtended);
8094

8195
/** @var static $param */
82-
$param = new static($parts['variableName'], $type, $description);
96+
$param = new static($variableName, $type, $description);
8397

8498
$param->setOptional(empty($parts['require']));
8599

@@ -131,6 +145,7 @@ public function getDefaultValue()
131145

132146
/**
133147
* Устанавливает значение по умолчанию
148+
*
134149
* @param $value
135150
*/
136151
public function setDefault($value)
@@ -159,6 +174,7 @@ public function getExampleValue()
159174

160175
/**
161176
* Устанавливает пример значения
177+
*
162178
* @param $value
163179
*/
164180
public function setExample($value)
@@ -178,6 +194,7 @@ public function isOptional()
178194

179195
/**
180196
* Устанавливает необязательность параметра
197+
*
181198
* @param $value
182199
*/
183200
public function setOptional($value)

src/DocBlock/ApiReturn.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use phpDocumentor\Reflection\DocBlock\Tags\Factory\StaticMethod;
99
use phpDocumentor\Reflection\Type;
1010
use phpDocumentor\Reflection\TypeResolver;
11-
use Tochka\JsonRpc\DocBlock\TypeResolver as CustomTypeResolver;
1211
use phpDocumentor\Reflection\Types\Context as TypeContext;
12+
use Tochka\JsonRpc\DocBlock\TypeResolver as CustomTypeResolver;
1313
use Webmozart\Assert\Assert;
1414

1515
/**
@@ -32,9 +32,9 @@ class ApiReturn extends BaseTag implements StaticMethod
3232
protected $is_root = false;
3333

3434
/**
35-
* @param string $variableName
36-
* @param Type $type
37-
* @param bool $isVariadic
35+
* @param string $variableName
36+
* @param Type $type
37+
* @param bool $isVariadic
3838
* @param Description $description
3939
*/
4040
public function __construct($variableName, Type $type = null, Description $description = null, $is_root = false)
@@ -62,8 +62,12 @@ public static function create(
6262

6363
preg_match(self::REGEXP, $body, $parts);
6464

65-
$descriptionStr = isset($parts['description']) ? trim($parts['description']) : '';
66-
$description = $descriptionFactory->create($descriptionStr, $context);
65+
$description = null;
66+
67+
if (null !== $descriptionFactory) {
68+
$descriptionStr = isset($parts['description']) ? trim($parts['description']) : '';
69+
$description = $descriptionFactory->create($descriptionStr, $context);
70+
}
6771

6872
$type = CustomTypeResolver::resolve(trim($parts['type']), $parts['typeFormat']);
6973

src/DocBlock/TypeResolver.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,49 @@
22

33
namespace Tochka\JsonRpc\DocBlock;
44

5-
use phpDocumentor\Reflection\Fqsen;
6-
use phpDocumentor\Reflection\Types\String_;
7-
use phpDocumentor\Reflection\Types\Integer;
5+
use phpDocumentor\Reflection\Types\Array_;
86
use phpDocumentor\Reflection\Types\Boolean;
97
use phpDocumentor\Reflection\Types\Float_;
10-
use phpDocumentor\Reflection\Types\Mixed;
11-
use phpDocumentor\Reflection\Types\Array_;
8+
use phpDocumentor\Reflection\Types\Integer;
9+
use phpDocumentor\Reflection\Types\Mixed_;
10+
use phpDocumentor\Reflection\Types\String_;
1211
use Tochka\JsonRpc\DocBlock\Types\Date;
1312
use Tochka\JsonRpc\DocBlock\Types\Enum;
1413
use Tochka\JsonRpc\DocBlock\Types\Object_;
1514

1615
class TypeResolver
1716
{
1817
protected static $keywords = [
19-
'string' => String_::class,
20-
'int' => Integer::class,
21-
'integer' => Integer::class,
22-
'bool' => Boolean::class,
23-
'boolean' => Boolean::class,
24-
'float' => Float_::class,
25-
'double' => Float_::class,
26-
'object' => Object_::class,
27-
'mixed' => Mixed::class,
28-
'array' => Array_::class,
29-
'date' => Date::class,
30-
'datetime' => Date::class,
31-
'enum' => Enum::class,
32-
'enumeration' => Enum::class
18+
'string' => String_::class,
19+
'int' => Integer::class,
20+
'integer' => Integer::class,
21+
'bool' => Boolean::class,
22+
'boolean' => Boolean::class,
23+
'float' => Float_::class,
24+
'double' => Float_::class,
25+
'object' => Object_::class,
26+
'mixed' => Mixed_::class,
27+
'array' => Array_::class,
28+
'date' => Date::class,
29+
'datetime' => Date::class,
30+
'enum' => Enum::class,
31+
'enumeration' => Enum::class,
3332
];
3433

3534
public static function resolve($type, $extended = null)
3635
{
3736
$lType = strtolower($type);
37+
3838
// проверим, вдруг это массив
39-
if (substr($type, -2) == '[]') {
40-
$type = static::resolve(substr($type, 0, -2));
39+
if (substr($type, -2) === '[]') {
40+
$type = static::resolve(substr($type, 0, -2), $extended);
4141

4242
return new Array_($type);
4343
}
4444

4545
// проверим специальные типы
4646
if (isset(self::$keywords[$lType])) {
47-
if (in_array(self::$keywords[$lType], [Date::class, Enum::class])) {
47+
if (\in_array(self::$keywords[$lType], [Date::class, Enum::class], true)) {
4848
return new self::$keywords[$lType]($extended);
4949
}
5050

src/DocBlock/Types/Enum.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,47 @@ class Enum implements Type
1414
use VariableValueTrait;
1515

1616
protected $variants;
17+
protected $type;
1718

1819
public function __construct($variants = null)
1920
{
20-
if (preg_match('/^\(.*\)$/iu', $variants)) {
21+
$this->type = 'string';
22+
23+
if (preg_match('/^\(.*\)$/u', $variants)) {
2124
$variants = trim($variants, '()');
2225

2326
preg_match_all('/(?<values>\"[^\"]*\"|[^,]+)/iu', $variants, $matches, PREG_PATTERN_ORDER);
2427

2528
$variants = [];
29+
$type = 0;
30+
2631
foreach ($matches['values'] as $value) {
27-
$variants[] = self::getRealValue($value);
32+
$realValue = self::getRealValue($value);
33+
34+
switch (true) {
35+
case \is_int($realValue):
36+
break;
37+
case \is_float($realValue):
38+
if ($type < 2) {
39+
$type = 1;
40+
}
41+
break;
42+
default:
43+
$type = 2;
44+
}
45+
46+
$variants[] = $realValue;
47+
}
48+
49+
switch ($type) {
50+
case 0:
51+
$this->type = 'int';
52+
break;
53+
case 1:
54+
$this->type = 'float';
55+
break;
56+
default:
57+
$this->type = 'string';
2858
}
2959
}
3060

@@ -36,6 +66,11 @@ public function getVariants()
3666
return $this->variants;
3767
}
3868

69+
public function getRealType()
70+
{
71+
return $this->type;
72+
}
73+
3974
/**
4075
* Returns a rendered output of the Type as it would be used in a DocBlock.
4176
*

src/DocBlock/VariableValueTrait.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ trait VariableValueTrait
77
protected static function getRealValue($value)
88
{
99
switch (strtolower($value)) {
10-
case 'true': return true;
11-
case 'false': return false;
12-
case 'null': return null;
13-
case '[]': return [];
10+
case 'true':
11+
return true;
12+
case 'false':
13+
return false;
14+
case 'null':
15+
return null;
16+
case '[]':
17+
return [];
1418
}
1519
if (is_numeric($value)) {
1620
return $value * 1;

0 commit comments

Comments
 (0)