Skip to content

Commit

Permalink
feat(TypeResolvers): Add support for alternative name in doc blocs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Czarnecki committed May 31, 2023
1 parent d5cc467 commit ec218d6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private function endsWith(string $statementClassToCheck, string $typeHintToSearc

private function isPrimitiveType(string $type): bool
{
return in_array($type, ['int', 'float', 'bool', 'string']);
return in_array($type, ['int', 'integer', 'float', 'bool', 'boolean', 'double', 'string']);
}

private function hasGlobalNamespacePrefix(string $typeHint): bool
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/DocBlockType/AlternativePHPDocsNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures\DocBlockType;

class AlternativePHPDocsNames
{
/** @var integer */
public $integer;

/** @var double */
public $double;

/** @var boolean */
public $boolean;
}
21 changes: 21 additions & 0 deletions tests/Metadata/Driver/DocBlockDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use JMS\Serializer\Metadata\Driver\DocBlockDriver;
use JMS\Serializer\Metadata\Driver\TypedPropertiesDriver;
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
use JMS\Serializer\Tests\Fixtures\DocBlockType\AlternativePHPDocsNames;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Collection\CollectionAsList;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Collection\CollectionOfClassesFromDifferentNamespace;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Collection\CollectionOfClassesFromDifferentNamespaceUsingGroupAlias;
Expand Down Expand Up @@ -462,4 +463,24 @@ public function testInferTypeForVirtualPropertyGetter()
$m->propertyMetadata['arrayOfStrings']->type
);
}

public function testAlternativeNames()
{
$m = $this->resolve(AlternativePHPDocsNames::class);

self::assertEquals(
['name' => 'integer', 'params' => []],
$m->propertyMetadata['integer']->type
);

self::assertEquals(
['name' => 'double', 'params' => []],
$m->propertyMetadata['double']->type
);

self::assertEquals(
['name' => 'boolean', 'params' => []],
$m->propertyMetadata['boolean']->type
);
}
}

0 comments on commit ec218d6

Please sign in to comment.