Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved phpstan types #6

Merged
merged 4 commits into from
Mar 6, 2023
Merged
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: 2 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: PHPStan

on:
pull_request:
branches: [ main ]
push:
paths:
- '**.php'
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
"providers": [
"Keepsuit\\LaravelTemporal\\LaravelTemporalServiceProvider"
]
},
"phpstan": {
"includes": [
"extension.neon"
]
}
},
"minimum-stability": "dev",
Expand Down
13 changes: 13 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
-
class: Keepsuit\LaravelTemporal\PHPStan\TemporalActivityProxyExtension
tags:
- phpstan.broker.methodsClassReflectionExtension
-
class: Keepsuit\LaravelTemporal\PHPStan\TemporalWorkflowProxyExtension
tags:
- phpstan.broker.methodsClassReflectionExtension

parameters:
stubFiles:
- stubs/phpstan/ActivityProxy.php.stub
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
includes:
- extension.neon
- phpstan-baseline.neon

parameters:
Expand All @@ -13,4 +14,4 @@ parameters:
checkGenericClassInNonGenericObjectType: false

ignoreErrors:
- '#Unable to resolve the template type TMergeRecursiveValue in call to method Illuminate\\Support\\Collection.*::mergeRecursive#'
- '#Unable to resolve the template type TMergeRecursiveValue in call to method Illuminate\\Support\\Collection.*::mergeRecursive#'
4 changes: 2 additions & 2 deletions src/Builder/ActivityBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public static function newLocal(): LocalActivityBuilder
* @template T of object
*
* @param class-string<T> $class
* @return ActivityProxy|T
* @return ActivityProxy<T>
*/
public function build(string $class)
public function build(string $class): ActivityProxy
{
return Temporal::getTemporalContext()->newActivityStub($class, $this->activityOptions);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Builder/LocalActivityBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public static function new(): LocalActivityBuilder
* @template T of object
*
* @param class-string<T> $class
* @return ActivityProxy|T
* @return ActivityProxy<T>
*/
public function build(string $class)
public function build(string $class): ActivityProxy
{
return Temporal::getTemporalContext()->newActivityStub($class, $this->activityOptions);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Builder/WorkflowBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public function withRunId(?string $runId): self
* @template T of object
*
* @param class-string<T> $class
* @return WorkflowProxy<T>|T
* @return WorkflowProxy<T>
*/
public function build(string $class)
public function build(string $class): WorkflowProxy
{
if ($this->runId !== null) {
return $this->getWorkflowClient()
Expand Down
150 changes: 150 additions & 0 deletions src/PHPStan/TemporalActivityProxyExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace Keepsuit\LaravelTemporal\PHPStan;

use PHPStan\Analyser\OutOfClassScope;
use PHPStan\Reflection\ClassMemberReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionVariant;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Type;
use Temporal\Internal\Transport\CompletableResultInterface;
use Temporal\Internal\Workflow\ActivityProxy;

class TemporalActivityProxyExtension implements MethodsClassReflectionExtension
{
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
if ($classReflection->getName() !== ActivityProxy::class) {
return false;
}

$activeTemplateTypeMap = $classReflection->getActiveTemplateTypeMap();

if ($activeTemplateTypeMap->count() !== 1) {
return false;
}

$objectType = $activeTemplateTypeMap->getType('T');

if ($objectType->isObject()->no()) {
return false;
}

if ($objectType->hasMethod($methodName)->no()) {
return false;
}

$methodReflection = $objectType->getMethod($methodName, new OutOfClassScope());

return $methodReflection->isPublic();
}

public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
{
$activeTemplateTypeMap = $classReflection->getActiveTemplateTypeMap();

$objectType = $activeTemplateTypeMap->getType('T');

$methodReflection = $objectType->getMethod($methodName, new OutOfClassScope());

$methodReturnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();

$returnType = new GenericObjectType(CompletableResultInterface::class, [$methodReturnType]);

return new class($classReflection, $methodName, $methodReflection, $returnType) implements MethodReflection
{
public function __construct(
protected ClassReflection $classReflection,
protected string $methodName,
protected MethodReflection $methodReflection,
protected Type $returnType
) {
}

public function isStatic(): bool
{
return false;
}

public function isPrivate(): bool
{
return false;
}

public function isPublic(): bool
{
return true;
}

public function getDocComment(): ?string
{
return null;
}

public function getName(): string
{
return $this->methodName;
}

public function getPrototype(): ClassMemberReflection
{
return $this;
}

public function getVariants(): array
{
$parameterAcceptor = ParametersAcceptorSelector::selectSingle($this->methodReflection->getVariants());

return [
new FunctionVariant(
$parameterAcceptor->getTemplateTypeMap(),
$parameterAcceptor->getResolvedTemplateTypeMap(),
$parameterAcceptor->getParameters(),
$parameterAcceptor->isVariadic(),
$this->returnType
),
];
}

public function isDeprecated(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function getDeprecatedDescription(): ?string
{
return null;
}

public function isFinal(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isInternal(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function getThrowType(): ?Type
{
return null;
}

public function hasSideEffects(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function getDeclaringClass(): ClassReflection
{
return $this->classReflection;
}
};
}
}
Loading