Skip to content

Commit 1cc81b7

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

File tree

5 files changed

+22
-34
lines changed

5 files changed

+22
-34
lines changed

src/Parser/AvroClassPropertyParser.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
namespace PhpKafka\PhpAvroSchemaGenerator\Parser;
44

55
use PhpKafka\PhpAvroSchemaGenerator\Exception\SkipPropertyException;
6-
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser;
76
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty;
87
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
98
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyType;
109
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyTypeItem;
10+
use PhpParser\Node\Stmt\Property;
1111

1212
/**
1313
* We will skip transient and private properties like starting at 'o_', '_', 'omitMandatoryCheck', 'allLazyKeysMarkedAsLoaded'
1414
*/
1515
class AvroClassPropertyParser extends ClassPropertyParser {
1616

1717
/**
18-
* @throws SkipPropertyException
18+
* @inheritdoc
1919
*/
20-
public function parseProperty($property): PhpClassPropertyInterface {
21-
$prop = parent::parseProperty($property);
20+
public function parseProperty(Property $property, ClassParserInterface $classParser): PhpClassPropertyInterface {
21+
$prop = parent::parseProperty($property, $classParser);
2222
if (str_starts_with($prop->getPropertyName(), 'o_') or str_starts_with($prop->getPropertyName(), '_')
2323
or in_array($prop->getPropertyName(), ['omitMandatoryCheck', 'allLazyKeysMarkedAsLoaded'])) {
2424
throw new SkipPropertyException();

src/Parser/ClassParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use ReflectionClass;
1919
use ReflectionException;
2020

21-
final class ClassParser implements ClassParserInterface
21+
class ClassParser implements ClassParserInterface
2222
{
2323
private ClassPropertyParserInterface $propertyParser;
2424
private Parser $parser;
@@ -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($pStatement, $this);
189189
}
190190
catch(SkipPropertyException $skip){ }
191191
}

src/Parser/ClassPropertyParser.php

Lines changed: 5 additions & 11 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,16 +30,11 @@ 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(Property $property, ClassParserInterface $classParser): PhpClassPropertyInterface
3836
{
39-
if (false === $property instanceof Property) {
40-
throw new RuntimeException(sprintf('Property must be of type: %s', Property::class));
41-
}
42-
43-
$propertyAttributes = $this->getPropertyAttributes($property);
37+
$propertyAttributes = $this->getPropertyAttributes($property, $classParser);
4438

4539
return new PhpClassProperty(
4640
$propertyAttributes['name'],
@@ -55,7 +49,7 @@ public function parseProperty($property): PhpClassPropertyInterface
5549
* @param Property $property
5650
* @return array<string, mixed>
5751
*/
58-
private function getPropertyAttributes(Property $property): array
52+
protected function getPropertyAttributes(Property $property): array
5953
{
6054
$attributes = $this->getEmptyAttributesArray();
6155
$docComments = $this->getAllPropertyDocComments($property);

src/Parser/ClassPropertyParserInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
interface ClassPropertyParserInterface
1212
{
1313
/**
14-
* @param Property|mixed $property
14+
* @param Property $property
15+
* @param ClassParserInterface $classParser
1516
* @return PhpClassPropertyInterface
1617
* @throws SkipPropertyException Such property will then just skipped
1718
*/
18-
public function parseProperty($property): PhpClassPropertyInterface;
19+
public function parseProperty(Property $property, ClassParserInterface $classParser): PhpClassPropertyInterface;
1920
}

tests/Unit/Parser/ClassPropertyParserTest.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace PhpKafka\PhpAvroSchemaGenerator\Tests\Unit\Parser;
66

7+
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser;
78
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser;
89
use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParserInterface;
910
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
1011
use PhpParser\Comment\Doc;
1112
use PhpParser\Node\Identifier;
1213
use PhpParser\Node\NullableType;
14+
use PhpParser\Node\Stmt\Class_;
1315
use PhpParser\Node\Stmt\Property;
1416
use PhpParser\Node\Stmt\PropertyProperty;
1517
use PhpParser\Node\UnionType;
@@ -20,6 +22,7 @@ class ClassPropertyParserTest extends TestCase
2022
{
2123
public function testParseProperty(): void
2224
{
25+
$classParser = $this->getMockBuilder(ClassParser::class)->disableOriginalConstructor()->getMock();
2326
$doc = $this->getMockBuilder(Doc::class)->disableOriginalConstructor()->getMock();
2427
$varId = $this->getMockBuilder(VarLikeIdentifier::class)->disableOriginalConstructor()->getMock();
2528
$varId->name = 'bla';
@@ -48,19 +51,9 @@ public function testParseProperty(): void
4851
$property4->props = [$propertyProperty];
4952
$cpp = new ClassPropertyParser($docParser);
5053

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));
54+
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property1, $classParser));
55+
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property2, $classParser));
56+
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property3, $classParser));
57+
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property4, $classParser));
5558
}
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-
}
59+
}

0 commit comments

Comments
 (0)