Skip to content

Commit

Permalink
php-kafka#33 Add ClassParser parameter into interface of parsing prop…
Browse files Browse the repository at this point in the history
…erty
  • Loading branch information
Pavel Alexeev authored and Pavel Alexeev committed Jan 8, 2022
1 parent cf63d4d commit 1cc81b7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 34 deletions.
8 changes: 4 additions & 4 deletions src/Parser/AvroClassPropertyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
namespace PhpKafka\PhpAvroSchemaGenerator\Parser;

use PhpKafka\PhpAvroSchemaGenerator\Exception\SkipPropertyException;
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyType;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyTypeItem;
use PhpParser\Node\Stmt\Property;

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

/**
* @throws SkipPropertyException
* @inheritdoc
*/
public function parseProperty($property): PhpClassPropertyInterface {
$prop = parent::parseProperty($property);
public function parseProperty(Property $property, ClassParserInterface $classParser): PhpClassPropertyInterface {
$prop = parent::parseProperty($property, $classParser);
if (str_starts_with($prop->getPropertyName(), 'o_') or str_starts_with($prop->getPropertyName(), '_')
or in_array($prop->getPropertyName(), ['omitMandatoryCheck', 'allLazyKeysMarkedAsLoaded'])) {
throw new SkipPropertyException();
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/ClassParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use ReflectionClass;
use ReflectionException;

final class ClassParser implements ClassParserInterface
class ClassParser implements ClassParserInterface
{
private ClassPropertyParserInterface $propertyParser;
private Parser $parser;
Expand Down Expand Up @@ -185,7 +185,7 @@ private function getAllClassProperties(Class_ $class, array $properties): array
foreach ($class->stmts as $pStatement) {
if ($pStatement instanceof Property) {
try {
$properties[] = $this->propertyParser->parseProperty($pStatement);
$properties[] = $this->propertyParser->parseProperty($pStatement, $this);
}
catch(SkipPropertyException $skip){ }
}
Expand Down
16 changes: 5 additions & 11 deletions src/Parser/ClassPropertyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace PhpKafka\PhpAvroSchemaGenerator\Parser;

use JetBrains\PhpStorm\Pure;
use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
Expand All @@ -14,9 +13,9 @@
use PhpParser\Comment\Doc;
use PhpParser\Node\Identifier;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\UnionType;
use RuntimeException;

class ClassPropertyParser implements ClassPropertyParserInterface
{
Expand All @@ -31,16 +30,11 @@ public function __construct(DocCommentParserInterface $docParser)
}

/**
* @param Property|mixed $property
* @return PhpClassPropertyInterface
* @inheritdoc
*/
public function parseProperty($property): PhpClassPropertyInterface
public function parseProperty(Property $property, ClassParserInterface $classParser): PhpClassPropertyInterface
{
if (false === $property instanceof Property) {
throw new RuntimeException(sprintf('Property must be of type: %s', Property::class));
}

$propertyAttributes = $this->getPropertyAttributes($property);
$propertyAttributes = $this->getPropertyAttributes($property, $classParser);

return new PhpClassProperty(
$propertyAttributes['name'],
Expand All @@ -55,7 +49,7 @@ public function parseProperty($property): PhpClassPropertyInterface
* @param Property $property
* @return array<string, mixed>
*/
private function getPropertyAttributes(Property $property): array
protected function getPropertyAttributes(Property $property): array
{
$attributes = $this->getEmptyAttributesArray();
$docComments = $this->getAllPropertyDocComments($property);
Expand Down
5 changes: 3 additions & 2 deletions src/Parser/ClassPropertyParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
interface ClassPropertyParserInterface
{
/**
* @param Property|mixed $property
* @param Property $property
* @param ClassParserInterface $classParser
* @return PhpClassPropertyInterface
* @throws SkipPropertyException Such property will then just skipped
*/
public function parseProperty($property): PhpClassPropertyInterface;
public function parseProperty(Property $property, ClassParserInterface $classParser): PhpClassPropertyInterface;
}
23 changes: 8 additions & 15 deletions tests/Unit/Parser/ClassPropertyParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace PhpKafka\PhpAvroSchemaGenerator\Tests\Unit\Parser;

use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser;
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser;
use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParserInterface;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
use PhpParser\Comment\Doc;
use PhpParser\Node\Identifier;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PhpParser\Node\UnionType;
Expand All @@ -20,6 +22,7 @@ class ClassPropertyParserTest extends TestCase
{
public function testParseProperty(): void
{
$classParser = $this->getMockBuilder(ClassParser::class)->disableOriginalConstructor()->getMock();
$doc = $this->getMockBuilder(Doc::class)->disableOriginalConstructor()->getMock();
$varId = $this->getMockBuilder(VarLikeIdentifier::class)->disableOriginalConstructor()->getMock();
$varId->name = 'bla';
Expand Down Expand Up @@ -48,19 +51,9 @@ public function testParseProperty(): void
$property4->props = [$propertyProperty];
$cpp = new ClassPropertyParser($docParser);

self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property1));
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property2));
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property3));
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property4));
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property1, $classParser));
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property2, $classParser));
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property3, $classParser));
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property4, $classParser));
}

public function testParsePropertyExceptionOnNonProperty(): void
{
self::expectException(\RuntimeException::class);
self::expectExceptionMessage('Property must be of type: PhpParser\Node\Stmt\Property');
$docParser = $this->getMockForAbstractClass(DocCommentParserInterface::class);
$cpp = new ClassPropertyParser($docParser);

$cpp->parseProperty(1);
}
}
}

0 comments on commit 1cc81b7

Please sign in to comment.