Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build:
tests-and-coverage:
environment:
php:
version: 7.1
version: 7.4
tests:
override:
- phpcs-run
Expand Down
9 changes: 0 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ sudo: false

language: php
php:
- 7.1
- 7.2
- 7.3
- 7.4

env:
Expand All @@ -13,12 +10,6 @@ env:
- SF_CACHE=4.3
- SF_CACHE=5

jobs:
exclude:
- php: 7.1
env:
- SF_CACHE=5

before_script:
- composer install --no-interaction -o
- if [ "$SF_CACHE" = "3" ]; then composer require symfony/cache:^3.3; fi;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">=7.1",
"php": ">=7.4",
"phpdocumentor/reflection-docblock": "^3.2.3|^4.0.1",
"tebru/php-type": "^0.1.7",
"tebru/doctrine-annotation-reader": "^0.3.7",
Expand Down
5 changes: 5 additions & 0 deletions src/Internal/TypeTokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public function create(
}
}

if ($property !== null && $property->getType() !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check to see if $property has the method getType() and support back to 7.1

$propertyType = TypeToken::create($property->getType()->getName());
return $this->checkGenericArray($propertyType, $property, $getterMethod, $setterMethod);
}

$type = $this->checkDocBlocks($property, $getterMethod, $setterMethod);
if ($type !== null) {
return $this->checkGenericArray(
Expand Down
5 changes: 1 addition & 4 deletions tests/Mock/AddressMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ class AddressMock
*/
private $street;

/**
* @var string
*/
private $city;
private string $city;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd make new mocks for 7.4 that we can combine later


/**
* @var string
Expand Down
2 changes: 1 addition & 1 deletion tests/Mock/AnnotatedMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AnnotatedMock
*/
private $fooBar;

private $fooBarBaz;
private float $fooBarBaz;

/**
* @Gson\VirtualProperty("vfoo")
Expand Down
2 changes: 2 additions & 0 deletions tests/Mock/ChildClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class ChildClass extends ChildClassParent
*/
private $default = 1;

private array $typedArray;

public function getBaz()
{
return $this->baz;
Expand Down
2 changes: 1 addition & 1 deletion tests/Mock/ChildClassParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ class ChildClassParent extends ChildClassParent2
* @QuxAnnotation("qux")
*/
private $foo;
private $bar;
private string $bar;
protected $baz;
}
2 changes: 1 addition & 1 deletion tests/Mock/ClassWithoutParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ClassWithoutParent
* @FooAnnotation("foo")
* @BarAnnotation("bar")
*/
private $foo;
private int $foo;

/**
* @FooAnnotation("foo2")
Expand Down
33 changes: 32 additions & 1 deletion tests/Mock/DocblockType/DocblockMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,18 @@ class DocblockMock
/**
* @var ArrayObject
*/
private $classGlobal;
private ArrayObject $classGlobal;

/**
* @var array
*/
private $array;

/**
* @var int[]
*/
private array $fullyTypedArray;

/**
* @var int[]
*/
Expand Down Expand Up @@ -160,6 +165,16 @@ class DocblockMock
*/
private $differentSetter;

/**
* @var ArrayObject
*/
private ArrayObject $differentGetterTyped;

/**
* @var ArrayObject
*/
private ArrayObject $differentSetterTyped;

/**
* @return int
*/
Expand Down Expand Up @@ -197,4 +212,20 @@ public function setDifferentSetter(array $foos): void
{
$this->differentSetter = $foos;
}

/**
* @return DocblockFoo[]
*/
public function getDifferentGetterTyped(): array
{
return $this->differentGetterTyped->getArrayCopy();
}

/**
* @param DocblockFoo[] $foos
*/
public function setDifferentSetterTyped(array $foos): void
{
$this->differentSetterTyped = new ArrayObject($foos);
}
}
2 changes: 1 addition & 1 deletion tests/Mock/ExcludedClassMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ class ExcludedClassMock
* @Type("Tebru\Gson\Test\Mock\GsonMock")
*/
private $foo;
private $bar;
private bool $bar;
}
5 changes: 1 addition & 4 deletions tests/Mock/UserMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ class UserMock
*/
private $password;

/**
* @var string
*/
private $name;
private string $name;

/**
* @var AddressMock
Expand Down
5 changes: 1 addition & 4 deletions tests/Mock/UserMockVirtual.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ class UserMockVirtual
*/
private $password;

/**
* @var string
*/
private $name;
private string $name;

/**
* @var AddressMock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function testCreateWithParents(): void
new ReflectionProperty(ChildClass::class, 'overridden'),
new ReflectionProperty(ChildClass::class, 'withTypehint'),
new ReflectionProperty(ChildClass::class, 'default'),
new ReflectionProperty(ChildClass::class, 'typedArray'),
new ReflectionProperty(ChildClassParent::class, 'baz'),
new ReflectionProperty(ChildClassParent2::class, 'qux'),
new ReflectionProperty(ChildClassParent::class, 'bar'),
Expand Down
56 changes: 56 additions & 0 deletions tests/Unit/Internal/TypeTokenFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,40 @@ public function testCreateFromAnnotation(): void
self::assertSame(ChildClass::class, $phpType->getRawType());
}

public function testCreateFromAnnotationWithTypedProperty(): void
{
$type = new Type(['value' => ChildClass::class]);
$annotations = new AnnotationCollection();
$annotations->add($type);

$factory = new TypeTokenFactory();
$property = new ReflectionProperty(ChildClass::class, 'typedArray');
$phpType = $factory->create($annotations, null, null, $property);

self::assertSame(ChildClass::class, $phpType->getRawType());
}

public function testCreateFromPropertyType(): void
{
$annotations = new AnnotationCollection();
$factory = new TypeTokenFactory();
$property = new ReflectionProperty(ChildClass::class, 'typedArray');
$phpType = $factory->create($annotations, null, null, $property);

self::assertSame('array', $phpType->getRawType());
}

public function testCreateFromArrayPropertyTypeWithGeneric(): void
{
$annotations = new AnnotationCollection();
$factory = new TypeTokenFactory();
$property = new ReflectionProperty(DocblockMock::class, 'fullyTypedArray');
$phpType = $factory->create($annotations, null, null, $property);

self::assertSame(TypeToken::HASH, $phpType->getRawType());
self::assertSame(TypeToken::INTEGER, $phpType->getGenerics()[0]->getRawType());
}

public function testCreateFromSetterTypehint(): void
{
$annotations = new AnnotationCollection();
Expand Down Expand Up @@ -360,6 +394,28 @@ public function testCreateFromDocblockDifferentSetter(): void
self::assertSame('array<Tebru\Gson\Test\Mock\DocblockType\DocblockFoo>', (string)$phpType);
}

public function testCreateFromDocblockDifferentGetterWithTypedProperty(): void
{
$annotations = new AnnotationCollection();
$factory = new TypeTokenFactory();
$property = new ReflectionProperty(DocblockMock::class, 'differentGetterTyped');
$getter = new ReflectionMethod(DocblockMock::class, 'getDifferentGetterTyped');
$phpType = $factory->create($annotations, $getter, null, $property);

self::assertSame('array<Tebru\Gson\Test\Mock\DocblockType\DocblockFoo>', (string)$phpType);
}

public function testCreateFromDocblockDifferentSetterWithTypedProperty(): void
{
$annotations = new AnnotationCollection();
$factory = new TypeTokenFactory();
$property = new ReflectionProperty(DocblockMock::class, 'differentSetterTyped');
$setter = new ReflectionMethod(DocblockMock::class, 'setDifferentSetterTyped');
$phpType = $factory->create($annotations, null, $setter, $property);

self::assertSame('array<Tebru\Gson\Test\Mock\DocblockType\DocblockFoo>', (string)$phpType);
}

public function testCreateFromGetter(): void
{
$annotations = new AnnotationCollection();
Expand Down