Skip to content

Commit 8bc1299

Browse files
authored
Merge pull request #2 from Paneon/develop
Release 1.1.0
2 parents 7d9926f + e745751 commit 8bc1299

File tree

6 files changed

+139
-17
lines changed

6 files changed

+139
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
======
33

4+
# 1.1.0
5+
6+
- Add support for psalm array syntax as well: `@var array<int>`
7+
48
# 1.0.0
59

610
- Initial Release of the Parser

src/Parser/PhpDocParser.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ class PhpDocParser
88
public const PROPERTY_TYPE_VARIABLE = 'VARIABLE';
99
public const PROPERTY_TYPE_METHOD = 'METHOD';
1010

11-
public function parseDocComment(string $phpDoc, $type = self::PROPERTY_TYPE_VARIABLE, $includeTypeNullable = false): string
12-
{
11+
public function parseDocComment(
12+
string $phpDoc,
13+
$type = self::PROPERTY_TYPE_VARIABLE,
14+
$includeTypeNullable = false
15+
): string {
1316
$varRegex = '/@var\s+(?P<var>[^\s*]+)?/';
1417
$methodRegex = '/@return\s+(?P<var>[^\s*]+)?/';
1518
$typeRegex = '/(?P<type>[^\[\]\s]+)(?P<array>\[\])?/i';
19+
$psalmTypeRegex = '/array\<(?P<psalmType>[^\s*]+)?\>/i';
1620

1721
if (empty($phpDoc)) {
1822
return 'any';
@@ -28,7 +32,12 @@ public function parseDocComment(string $phpDoc, $type = self::PROPERTY_TYPE_VARI
2832
foreach ($types as $phpType) {
2933
$tsType = $phpType;
3034

31-
if (preg_match($typeRegex, $phpType, $typeMatch)) {
35+
if (preg_match($psalmTypeRegex, $phpType, $typeMatch)) {
36+
$tsType = $this->getTypeEquivalent($typeMatch['psalmType'], $includeTypeNullable);
37+
if ($tsType !== null) {
38+
$tsType .= '[]';
39+
}
40+
} else if (preg_match($typeRegex, $phpType, $typeMatch)) {
3241
$tsType = $this->getTypeEquivalent($typeMatch['type'], $includeTypeNullable);
3342
if ($tsType === null) {
3443
continue;

tests/Fixtures/ArrayClass.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Paneon\PhpToTypeScript\Tests\Fixtures;
4+
5+
use DateTime;
6+
use Paneon\PhpToTypeScript\Annotation as PTS;
7+
8+
/**
9+
* @PTS\TypeScriptInterface
10+
*/
11+
class ArrayClass
12+
{
13+
/**
14+
* @PTS\Exclude()
15+
*
16+
* @var bool[]
17+
*/
18+
protected $excluded;
19+
20+
/**
21+
* @var mixed[]
22+
*/
23+
protected $mixedArray;
24+
25+
/**
26+
* @var SomeClass[]
27+
*/
28+
protected $classCollection;
29+
30+
/**
31+
* @var mixed
32+
*/
33+
protected $mixed;
34+
35+
/**
36+
* @PTS\Type("ClassImplementingInterface1[]|ClassImplementingInterface2[]")
37+
*/
38+
protected $someInterfaceArray;
39+
40+
/**
41+
* This syntax is not correct PHPDoc actually, but anyway used sometimes.
42+
*
43+
* @var array<int>
44+
*/
45+
protected $psalmArrayType;
46+
47+
/**
48+
* @PTS\VirtualProperty()
49+
* @return bool[]
50+
*/
51+
public function hasSomeValue()
52+
{
53+
return [true];
54+
}
55+
56+
/**
57+
* @PTS\VirtualProperty()
58+
*/
59+
public function virtualWithReturnType(): int
60+
{
61+
return 1;
62+
}
63+
}

tests/Fixtures/Person.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ class Person
8383
*/
8484
protected $someInterfaceArray;
8585

86+
/**
87+
* This syntax is not correct PHPDoc actually, but anyway used sometimes.
88+
*
89+
* @var array<int>
90+
*/
91+
protected $psalmArrayType;
92+
8693
/**
8794
* @PTS\VirtualProperty()
8895
* @return bool
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Paneon\PhpToTypeScript\Tests\Services;
4+
5+
use Paneon\PhpToTypeScript\Tests\AbstractTestCase;
6+
use ReflectionException;
7+
8+
class ParserServiceArrayTest extends AbstractTestCase
9+
{
10+
public function testConvertsMixedToArray()
11+
{
12+
$content = $this->loadFixture();
13+
14+
$this->assertStringContainsString('mixed: any;', $content);
15+
$this->assertStringContainsString('mixedArray: any[];', $content);
16+
}
17+
18+
public function testRespectsTypeScriptTypeAnnotationForArrays()
19+
{
20+
$content = $this->loadFixture();
21+
22+
$this->assertStringContainsString('someInterfaceArray: ClassImplementingInterface1[]|ClassImplementingInterface2[];', $content);
23+
}
24+
25+
public function testPhpDocArraySyntax()
26+
{
27+
$content = $this->loadFixture();
28+
29+
$this->assertStringContainsString('classCollection: SomeClass[];', $content);
30+
}
31+
32+
public function testPsalmArraySyntax()
33+
{
34+
$content = $this->loadFixture();
35+
36+
$this->assertStringContainsString('psalmArrayType: number[];', $content);
37+
}
38+
39+
private function loadFixture(): ?string
40+
{
41+
$fixture = $this->getDefaultFixtureFile();
42+
return $this->parserService->getInterfaceContent($fixture);
43+
}
44+
45+
private function getDefaultFixtureFile(): string
46+
{
47+
return __DIR__ . '/../Fixtures/ArrayClass.php';
48+
}
49+
}

tests/ParserServiceTest.php renamed to tests/Services/ParserServiceTest.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22

3-
namespace Paneon\PhpToTypeScript\Tests;
3+
namespace Paneon\PhpToTypeScript\Tests\Services;
44

5+
use Paneon\PhpToTypeScript\Tests\AbstractTestCase;
56
use ReflectionException;
67

78
class ParserServiceTest extends AbstractTestCase
@@ -58,23 +59,12 @@ public function addsPrefixAndSuffixToClassInstances()
5859
*/
5960
public function shouldNotBreakWhenTryingToParseATrait()
6061
{
61-
$fixture = __DIR__ . '/Fixtures/SomeTrait.php';
62+
$fixture = __DIR__ . '/../Fixtures/SomeTrait.php';
6263
$content = $this->parserService->getInterfaceContent($fixture);
6364

6465
$this->assertNull($content);
6566
}
6667

67-
/**
68-
* @test
69-
*/
70-
public function convertsMixedToArray()
71-
{
72-
$fixture = $this->getDefaultFixtureFile();
73-
$content = $this->parserService->getInterfaceContent($fixture);
74-
75-
$this->assertStringContainsString('mixed: any;', $content);
76-
$this->assertStringContainsString('mixedArray: any[];', $content);
77-
}
7868

7969
/**
8070
* @test
@@ -159,6 +149,6 @@ private function loadFixture(): ?string
159149

160150
private function getDefaultFixtureFile(): string
161151
{
162-
return __DIR__ . '/Fixtures/Person.php';
152+
return __DIR__ . '/../Fixtures/Person.php';
163153
}
164154
}

0 commit comments

Comments
 (0)