Skip to content

Commit

Permalink
Merge pull request #165 from Jean85/use-stubs
Browse files Browse the repository at this point in the history
Use generics
  • Loading branch information
localheinz authored May 8, 2020
2 parents 58bb62e + b8ba301 commit 5f8e53c
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 150 deletions.
3 changes: 2 additions & 1 deletion .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ $config->getFinder()
'.build/',
'.github/',
])
->name('.php_cs');
->name('.php_cs')
->name('*.phpstub');

$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php_cs.cache');

Expand Down
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

For a full diff see [`0.6.2...master`][0.6.2...master].
For a full diff see [`0.7.0...master`][0.7.0...master].

## [`0.7.0`][0.7.0]

For a full diff see [`0.6.2...0.7.0`][0.6.2...0.7.0].

### Changed

* Moved `src/extension.neon` to `extension.neon` ([#140]), by [@localheinz]
* Marked classes as `@internal` ([#144]), by [@localheinz]
* Moved and renamed internal classes ([#153]), by [@localheinz]
* Required `phpstan/phpstan:~0.12.6` ([#165]), by [@Jean85]
* Simplified extension by making use of generics ([#165]), by [@Jean85] and [@ondrejmirtes]

## [`0.6.2`][0.6.2]

Expand Down Expand Up @@ -101,6 +107,7 @@ For a full diff see [`afd6fd9...0.1`][afd6fd9...0.1].
[0.6.0]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.6.0
[0.6.1]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.6.1
[0.6.2]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.6.2
[0.7.0]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.7.0

[afd6fd9...0.1]: https://github.com/Jan0707/phpstan-prophecy/compare/afd6fd9...0.1
[0.1...0.1.1]: https://github.com/Jan0707/phpstan-prophecy/compare/0.1...0.1.1
Expand All @@ -115,7 +122,8 @@ For a full diff see [`afd6fd9...0.1`][afd6fd9...0.1].
[0.5.1...0.6.0]: https://github.com/Jan0707/phpstan-prophecy/compare/0.5.1...0.6.0
[0.6.0...0.6.1]: https://github.com/Jan0707/phpstan-prophecy/compare/0.6.0...0.6.1
[0.6.1...0.6.2]: https://github.com/Jan0707/phpstan-prophecy/compare/0.6.1...0.6.2
[0.6.2...master]: https://github.com/Jan0707/phpstan-prophecy/compare/0.6.2...master
[0.6.2...0.7.0]: https://github.com/Jan0707/phpstan-prophecy/compare/0.6.2...0.7.0
[0.7.0...master]: https://github.com/Jan0707/phpstan-prophecy/compare/0.7.0...master

[#67]: https://github.com/Jan0707/phpstan-prophecy/pull/67
[#79]: https://github.com/Jan0707/phpstan-prophecy/pull/79
Expand All @@ -127,6 +135,9 @@ For a full diff see [`afd6fd9...0.1`][afd6fd9...0.1].
[#140]: https://github.com/Jan0707/phpstan-prophecy/pull/140
[#144]: https://github.com/Jan0707/phpstan-prophecy/pull/144
[#153]: https://github.com/Jan0707/phpstan-prophecy/pull/153
[#165]: https://github.com/Jan0707/phpstan-prophecy/pull/165

[@Jean85]: https://github.com/Jean85
[@localheinz]: https://github.com/localheinz
[@ondrejmirtes ]: https://github.com/ondrejmirtes
[@PedroTroller]: https://github.com/PedroTroller
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "^7.1",
"phpstan/phpstan": "^0.12.0"
"phpstan/phpstan": "^0.12.6"
},
"conflict": {
"phpspec/prophecy": "<1.7.0,>=2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 11 additions & 12 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
parameters:
stubFiles:
- stubs/ObjectProphecy.phpstub
- stubs/ProphecyInterface.phpstub

services:
-
class: JanGregor\Prophecy\PhpDoc\ObjectProphecy\TypeNodeResolverExtension
Expand All @@ -10,28 +15,22 @@ services:
- phpstan.broker.methodsClassReflectionExtension

-
class: JanGregor\Prophecy\Type\Prophet\ProphesizeDynamicReturnTypeExtension
arguments:
className: Prophecy\Prophet
class: JanGregor\Prophecy\Type\ObjectProphecy\WillExtendOrImplementDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
autowired: false

-
class: JanGregor\Prophecy\Type\Prophet\ProphesizeDynamicReturnTypeExtension
arguments:
className: PHPUnit\Framework\TestCase
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
className: Prophecy\Prophet
autowired: false

-
class: JanGregor\Prophecy\Type\ObjectProphecy\RevealDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: JanGregor\Prophecy\Type\ObjectProphecy\WillExtendOrImplementDynamicReturnTypeExtension
class: JanGregor\Prophecy\Type\Prophet\ProphesizeDynamicReturnTypeExtension
arguments:
className: PHPUnit\Framework\TestCase
autowired: false
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

10 changes: 7 additions & 3 deletions src/PhpDoc/ObjectProphecy/TypeNodeResolverExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace JanGregor\Prophecy\PhpDoc\ObjectProphecy;

use JanGregor\Prophecy\Type\ObjectProphecy;
use PHPStan\Analyser;
use PHPStan\PhpDoc;
use PHPStan\PhpDocParser;
Expand All @@ -37,7 +36,7 @@ public function setTypeNodeResolver(PhpDoc\TypeNodeResolver $typeNodeResolver):

public function getCacheKey(): string
{
return 'prophecy-v1';
return 'prophecy-with-generics-v1';
}

public function resolve(PhpDocParser\Ast\Type\TypeNode $typeNode, Analyser\NameScope $nameScope): ?Type\Type
Expand Down Expand Up @@ -65,7 +64,12 @@ public function resolve(PhpDocParser\Ast\Type\TypeNode $typeNode, Analyser\NameS
}

if (null !== $objectProphecyType && null !== $prophesizedType) {
return new ObjectProphecy\ObjectProphecyType($prophesizedType->getClassName());
return new Type\Generic\GenericObjectType(
Prophecy\ObjectProphecy::class,
[
$prophesizedType,
]
);
}
}

Expand Down
64 changes: 0 additions & 64 deletions src/Type/ObjectProphecy/ObjectProphecyType.php

This file was deleted.

59 changes: 0 additions & 59 deletions src/Type/ObjectProphecy/RevealDynamicReturnTypeExtension.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Reflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type;
use Prophecy\Prophecy;

/**
* @internal
Expand Down Expand Up @@ -54,7 +55,10 @@ public function getTypeFromMethodCall(

$returnType = $parametersAcceptor->getReturnType();

if (!$calledOnType instanceof ObjectProphecyType) {
if (
!$calledOnType instanceof Type\Generic\GenericObjectType
|| Prophecy\ObjectProphecy::class !== $calledOnType->getClassName()
) {
return $returnType;
}

Expand Down Expand Up @@ -82,8 +86,14 @@ public function getTypeFromMethodCall(
$className = $scope->getClassReflection()->getName();
}

$calledOnType->addProphesizedClass($className);

return $calledOnType;
return new Type\Generic\GenericObjectType(
Prophecy\ObjectProphecy::class,
[
Type\TypeCombinator::intersect(
new Type\ObjectType($className),
...$calledOnType->getTypes()
),
]
);
}
}
14 changes: 11 additions & 3 deletions src/Type/Prophet/ProphesizeDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

namespace JanGregor\Prophecy\Type\Prophet;

use JanGregor\Prophecy\Type\ObjectProphecy;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Reflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type;
use Prophecy\Prophecy;

/**
* @internal
Expand Down Expand Up @@ -52,7 +52,10 @@ public function getTypeFromMethodCall(
$returnType = $parametersAcceptor->getReturnType();

if (0 === \count($methodCall->args)) {
return new ObjectProphecy\ObjectProphecyType();
return new Type\Generic\GenericObjectType(
Prophecy\ObjectProphecy::class,
[]
);
}

$argumentType = $scope->getType($methodCall->args[0]->value);
Expand All @@ -75,6 +78,11 @@ public function getTypeFromMethodCall(
$className = $scope->getClassReflection()->getName();
}

return new ObjectProphecy\ObjectProphecyType($className);
return new Type\Generic\GenericObjectType(
Prophecy\ObjectProphecy::class,
[
new Type\ObjectType($className),
]
);
}
}
28 changes: 28 additions & 0 deletions stubs/ObjectProphecy.phpstub
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018 Jan Gregor Emge-Triebel
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/Jan0707/phpstan-prophecy
*/

namespace Prophecy\Prophecy;

/**
* @template T of object
* @template-implements ProphecyInterface<T>
*/
class ObjectProphecy implements ProphecyInterface
{
/**
* @return T
*/
public function reveal()
{
}
}
Loading

0 comments on commit 5f8e53c

Please sign in to comment.