Skip to content

Commit 5f11244

Browse files
Pavel AlexeevPavel Alexeev
Pavel Alexeev
authored and
Pavel Alexeev
committed
#33 Add Class_ parameter into interface of parsing property
1 parent cf63d4d commit 5f11244

File tree

5 files changed

+21
-30
lines changed

5 files changed

+21
-30
lines changed

src/Parser/AvroClassPropertyParser.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
99
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyType;
1010
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyTypeItem;
11+
use PhpParser\Node\Stmt\Class_;
12+
use PhpParser\Node\Stmt\Property;
1113

1214
/**
1315
* We will skip transient and private properties like starting at 'o_', '_', 'omitMandatoryCheck', 'allLazyKeysMarkedAsLoaded'
1416
*/
1517
class AvroClassPropertyParser extends ClassPropertyParser {
1618

1719
/**
18-
* @throws SkipPropertyException
20+
* @inheritdoc
21+
* @override
1922
*/
20-
public function parseProperty($property): PhpClassPropertyInterface {
21-
$prop = parent::parseProperty($property);
23+
public function parseProperty(Class_ $class, Property $property): PhpClassPropertyInterface {
24+
$prop = parent::parseProperty($class, $property);
2225
if (str_starts_with($prop->getPropertyName(), 'o_') or str_starts_with($prop->getPropertyName(), '_')
2326
or in_array($prop->getPropertyName(), ['omitMandatoryCheck', 'allLazyKeysMarkedAsLoaded'])) {
2427
throw new SkipPropertyException();

src/Parser/ClassParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private function getAllClassProperties(Class_ $class, array $properties): array
185185
foreach ($class->stmts as $pStatement) {
186186
if ($pStatement instanceof Property) {
187187
try {
188-
$properties[] = $this->propertyParser->parseProperty($pStatement);
188+
$properties[] = $this->propertyParser->parseProperty($class, $pStatement);
189189
}
190190
catch(SkipPropertyException $skip){ }
191191
}

src/Parser/ClassPropertyParser.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PhpKafka\PhpAvroSchemaGenerator\Parser;
66

7-
use JetBrains\PhpStorm\Pure;
87
use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro;
98
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty;
109
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
@@ -14,9 +13,9 @@
1413
use PhpParser\Comment\Doc;
1514
use PhpParser\Node\Identifier;
1615
use PhpParser\Node\NullableType;
16+
use PhpParser\Node\Stmt\Class_;
1717
use PhpParser\Node\Stmt\Property;
1818
use PhpParser\Node\UnionType;
19-
use RuntimeException;
2019

2120
class ClassPropertyParser implements ClassPropertyParserInterface
2221
{
@@ -31,15 +30,10 @@ public function __construct(DocCommentParserInterface $docParser)
3130
}
3231

3332
/**
34-
* @param Property|mixed $property
35-
* @return PhpClassPropertyInterface
33+
* @inheritdoc
3634
*/
37-
public function parseProperty($property): PhpClassPropertyInterface
35+
public function parseProperty(Class_ $class, Property $property): PhpClassPropertyInterface
3836
{
39-
if (false === $property instanceof Property) {
40-
throw new RuntimeException(sprintf('Property must be of type: %s', Property::class));
41-
}
42-
4337
$propertyAttributes = $this->getPropertyAttributes($property);
4438

4539
return new PhpClassProperty(

src/Parser/ClassPropertyParserInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66

77
use PhpKafka\PhpAvroSchemaGenerator\Exception\SkipPropertyException;
88
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
9+
use PhpParser\Node\Stmt\Class_;
910
use PhpParser\Node\Stmt\Property;
1011

1112
interface ClassPropertyParserInterface
1213
{
1314
/**
14-
* @param Property|mixed $property
15+
* @param Class_ $class
16+
* @param Property $property
1517
* @return PhpClassPropertyInterface
1618
* @throws SkipPropertyException Such property will then just skipped
1719
*/
18-
public function parseProperty($property): PhpClassPropertyInterface;
20+
public function parseProperty(Class_ $class, Property $property): PhpClassPropertyInterface;
1921
}

tests/Unit/Parser/ClassPropertyParserTest.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Comment\Doc;
1111
use PhpParser\Node\Identifier;
1212
use PhpParser\Node\NullableType;
13+
use PhpParser\Node\Stmt\Class_;
1314
use PhpParser\Node\Stmt\Property;
1415
use PhpParser\Node\Stmt\PropertyProperty;
1516
use PhpParser\Node\UnionType;
@@ -20,6 +21,7 @@ class ClassPropertyParserTest extends TestCase
2021
{
2122
public function testParseProperty(): void
2223
{
24+
$class = $this->getMockBuilder(Class_::class)->disableOriginalConstructor()->getMock();
2325
$doc = $this->getMockBuilder(Doc::class)->disableOriginalConstructor()->getMock();
2426
$varId = $this->getMockBuilder(VarLikeIdentifier::class)->disableOriginalConstructor()->getMock();
2527
$varId->name = 'bla';
@@ -48,19 +50,9 @@ public function testParseProperty(): void
4850
$property4->props = [$propertyProperty];
4951
$cpp = new ClassPropertyParser($docParser);
5052

51-
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property1));
52-
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property2));
53-
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property3));
54-
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property4));
53+
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($class, $property1));
54+
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($class, $property2));
55+
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($class, $property3));
56+
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($class, $property4));
5557
}
56-
57-
public function testParsePropertyExceptionOnNonProperty(): void
58-
{
59-
self::expectException(\RuntimeException::class);
60-
self::expectExceptionMessage('Property must be of type: PhpParser\Node\Stmt\Property');
61-
$docParser = $this->getMockForAbstractClass(DocCommentParserInterface::class);
62-
$cpp = new ClassPropertyParser($docParser);
63-
64-
$cpp->parseProperty(1);
65-
}
66-
}
58+
}

0 commit comments

Comments
 (0)