Skip to content
Open
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
6 changes: 3 additions & 3 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
pull_request:
paths: &paths
- 'src/**'
- 'psalm.xml'
- 'phpstan.neon'
- 'composer.json'
- '.github/workflows/static.yml'
push:
Expand All @@ -19,8 +19,8 @@ concurrency:
cancel-in-progress: true

jobs:
psalm:
uses: yiisoft/actions/.github/workflows/psalm.yml@master
phpstan:
uses: yiisoft/actions/.github/workflows/phpstan.yml@master
with:
os: >-
['ubuntu-latest']
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.2.1 under development

- Chg #92: Replace Psalm with PHPStan (@vjik)
- Enh #92: Remove redundant `method_exists()` check for `ReflectionMethod::getTentativeReturnType()` (@vjik)
- Enh #92: Remove unreachable union type handling in intersection type rendering (@vjik)
- Enh #85: Explicitly import functions and constants in "use" section (@vjik)

## 1.2.0 April 02, 2026
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
[![Code Coverage](https://codecov.io/gh/yiisoft/proxy/branch/master/graph/badge.svg)](https://codecov.io/gh/yiisoft/proxy)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fproxy%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/proxy/master)
[![static analysis](https://github.com/yiisoft/proxy/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/proxy/actions?query=workflow%3A%22static+analysis%22)
[![type-coverage](https://shepherd.dev/github/yiisoft/proxy/coverage.svg)](https://shepherd.dev/github/yiisoft/proxy)

The package is able to build generic proxy for a class i.e. it allows intercepting all class method calls. It's used in
[yii-debug](https://github.com/yiisoft/yii-debug) package to collect service's method calls information.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.95",
"infection/infection": "^0.29.8 || ^0.35.8",
"phpstan/phpstan": "^2.3.7",
"phpunit/phpunit": "^10.5.47",
"rector/rector": "^2.5",
"shipmonk/composer-dependency-analyser": "^1.8",
"vimeo/psalm": "^5.26.1 || ^6.12",
"yiisoft/code-style": "^1.1"
},
"autoload": {
Expand All @@ -52,6 +52,7 @@
"cs-fix": "php-cs-fixer fix",
"dependency-analyser": "composer-dependency-analyser",
"phan": "phan --progress-bar -o analysis.txt",
"phpstan": "phpstan",
"rector": "rector",
"test": "phpunit --testdox --no-interaction"
},
Expand Down
4 changes: 2 additions & 2 deletions docs/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ The package tests are checked with [Infection](https://infection.github.io/) mut

## Static analysis

The code is statically analyzed with [Psalm](https://psalm.dev/). To run static analysis:
The code is statically analyzed with [PHPStan](https://phpstan.org/). To run static analysis:

```shell
./vendor/bin/psalm
./vendor/bin/phpstan
```

## Dependencies
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: max
paths:
- src
treatPhpDocTypesAsCertain: false
16 changes: 0 additions & 16 deletions psalm.xml

This file was deleted.

53 changes: 23 additions & 30 deletions src/ClassConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
/**
* Gets single class config based for individual class.
*
* @param string $className Full class or interface name (including namespace).
*
* @psalm-param class-string $className
* @param class-string $className Full class or interface name (including namespace).
*
* @throws InvalidArgumentException In case class or interface does not exist.
*
Expand All @@ -42,14 +40,16 @@
{
try {
$reflection = new ReflectionClass($className);
} catch (ReflectionException) {
throw new InvalidArgumentException("$className must exist.");
}

/**
* @psalm-suppress MixedArgumentTypeCoercion Can be removed after release
* https://github.com/vimeo/psalm/pull/8405
* PHPStan treats `class-string` as an existing class name even with `treatPhpDocTypesAsCertain: false`.
* @see https://github.com/phpstan/phpstan/issues/5502
* @phpstan-ignore catch.neverThrown
*/
catch (ReflectionException) {
throw new InvalidArgumentException("$className must exist.");
}

return new ClassConfig(
isInterface: $reflection->isInterface(),
namespace: $reflection->getNamespaceName(),
Expand All @@ -65,10 +65,9 @@
/**
* Gets the complete set of method configs for a given class reflection.
*
* @param ReflectionClass $class Reflection of a class.
* @param ReflectionClass<object> $class Reflection of a class.
*
* @return MethodConfig[] List of method configs. The order is maintained.
* @psalm-return array<string,MethodConfig>
* @return array<string, MethodConfig> List of method configs. The order is maintained.
*/
private function getMethodConfigs(ReflectionClass $class): array
{
Expand All @@ -83,7 +82,7 @@
/**
* Gets single method config for individual class / method reflection pair.
*
* @param ReflectionClass $class Reflection of a class.
* @param ReflectionClass<object> $class Reflection of a class.
* @param ReflectionMethod $method Reflection of a method.
*
* @return MethodConfig Single method config.
Expand All @@ -101,7 +100,7 @@
/**
* Gets the set of method modifiers for a given class / method reflection pair
*
* @param ReflectionClass $class Reflection of a class.
* @param ReflectionClass<object> $class Reflection of a class.
* @param ReflectionMethod $method Reflection of a method.
*
* @return string[] List of method modifiers.
Expand All @@ -126,8 +125,7 @@
*
* @param ReflectionMethod $method Reflection of a method.
*
* @return ParameterConfig[] List of parameter configs. The order is maintained.
* @psalm-return array<string,ParameterConfig>
* @return array<string, ParameterConfig> List of parameter configs. The order is maintained.
*/
private function getMethodParameterConfigs(ReflectionMethod $method): array
{
Expand Down Expand Up @@ -196,17 +194,11 @@
*/
private function getMethodReturnTypeConfig(ReflectionMethod $method): ?TypeConfig
{
$returnType = $method->getReturnType();
if (!$returnType && method_exists($method, 'getTentativeReturnType')) {
$returnType = $method->getTentativeReturnType();
}
/**
* @var ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null $returnType
*/

if (!$returnType) {
$returnType = $method->getReturnType() ?? $method->getTentativeReturnType();
if ($returnType === null) {
return null;
}
/** @var ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType $returnType */

return new TypeConfig(
name: $this->convertTypeToString($returnType, $method->getDeclaringClass()->getName()),
Expand Down Expand Up @@ -234,7 +226,6 @@
{
$types = array_map(
function (ReflectionNamedType|ReflectionIntersectionType $subType) use ($declaringClassName) {
/** @psalm-suppress DocblockTypeContradiction */
if ($subType instanceof ReflectionIntersectionType) {
return '(' . $this->getIntersectionType($subType, $declaringClassName) . ')';
}
Expand All @@ -249,14 +240,16 @@

private function getIntersectionType(ReflectionIntersectionType $type, ?string $declaringClassName = null): string
{
$types = array_map(

Check warning on line 243 in src/ClassConfigFactory.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMap": @@ @@ } private function getIntersectionType(ReflectionIntersectionType $type, ?string $declaringClassName = null): string { - $types = array_map( - /** - * `ReflectionIntersectionType::getTypes()` is typed as `ReflectionType[]` in the stubs bundled with - * PHPStan, although intersection parts are always `ReflectionNamedType`. - * @see https://github.com/phpstan/phpstan/issues/15809 - * @PHPStan-Ignore argument.type - */ - function (ReflectionNamedType $subType) use ($declaringClassName) { - $name = $subType->getName(); - return $declaringClassName !== null && $name === $declaringClassName ? 'self' : $name; - }, - $type->getTypes() - ); + $types = $type->getTypes(); return implode('&', $types); } }
function (ReflectionNamedType|ReflectionUnionType $subType) use ($declaringClassName) {
/** @psalm-suppress DocblockTypeContradiction */
if ($subType instanceof ReflectionUnionType) {
return '(' . $this->getUnionType($subType, $declaringClassName) . ')';
}
/**
* `ReflectionIntersectionType::getTypes()` is typed as `ReflectionType[]` in the stubs bundled with
* PHPStan, although intersection parts are always `ReflectionNamedType`.
* @see https://github.com/phpstan/phpstan/issues/15809
* @phpstan-ignore argument.type
*/
function (ReflectionNamedType $subType) use ($declaringClassName) {
$name = $subType->getName();
return ($declaringClassName !== null && $name === $declaringClassName) ? 'self' : $name;

Check warning on line 252 in src/ClassConfigFactory.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndAllSubExprNegation": @@ @@ */ function (ReflectionNamedType $subType) use ($declaringClassName) { $name = $subType->getName(); - return $declaringClassName !== null && $name === $declaringClassName ? 'self' : $name; + return !($declaringClassName !== null) && !($name === $declaringClassName) ? 'self' : $name; }, $type->getTypes() );

Check warning on line 252 in src/ClassConfigFactory.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "NotIdentical": @@ @@ */ function (ReflectionNamedType $subType) use ($declaringClassName) { $name = $subType->getName(); - return $declaringClassName !== null && $name === $declaringClassName ? 'self' : $name; + return $declaringClassName === null && $name === $declaringClassName ? 'self' : $name; }, $type->getTypes() );
},
$type->getTypes(),
);
Expand Down
3 changes: 1 addition & 2 deletions src/ClassRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,8 @@ private function renderType(TypeConfig $type): string
/**
* Renders parameters passed to a proxy's method call.
*
* @param ParameterConfig[] $parameters A map where key is a {@see ParameterConfig::$name} and value is
* @param array<string, ParameterConfig> $parameters A map where key is a {@see ParameterConfig::$name} and value is
* {@see ParameterConfig} instance.
* @psalm-param array<string, ParameterConfig> $parameters
*
* @return string Parameters as a string. Empty string is returned when no parameters were passed.
*/
Expand Down
3 changes: 1 addition & 2 deletions src/Config/ClassConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public function __construct(
*/
public array $interfaces,
/**
* @var MethodConfig[] A map where key is a {@see $name} and value is {@see MethodConfig} instance.
* @psalm-var array<string, MethodConfig>
* @var array<string, MethodConfig> A map where key is a {@see $name} and value is {@see MethodConfig} instance.
*/
public array $methods,
) {}
Expand Down
7 changes: 3 additions & 4 deletions src/Config/MethodConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ public function __construct(
*/
public string $name,
/**
* @var ParameterConfig[] A map where key is a {@see ParameterConfig::$name} and value is {@see ParameterConfig}
* instance.
* @psalm-var array<string, ParameterConfig>
* @var array<string, ParameterConfig> A map where key is a {@see ParameterConfig::$name} and value is
* {@see ParameterConfig} instance.
*/
public array $parameters,
/**
Expand All @@ -38,7 +37,7 @@ public function __construct(
*
* @return bool `true` if return type specified and `false` otherwise.
*
* @psalm-assert-if-true TypeConfig $this->returnType
* @phpstan-assert-if-true TypeConfig $this->returnType
*/
public function hasReturnType(): bool
{
Expand Down
2 changes: 1 addition & 1 deletion src/Config/ParameterConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(
*
* @return bool `true` if type specified and `false` otherwise.
*
* @psalm-assert-if-true TypeConfig $this->type
* @phpstan-assert-if-true TypeConfig $this->type
*/
public function hasType(): bool
{
Expand Down
14 changes: 5 additions & 9 deletions src/ObjectProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

/**
* Base proxy class for objects to use in {@see ProxyManager}. A concrete implementation can be provided too.
* @psalm-suppress ClassMustBeFinal
*/
class ObjectProxy
{
Expand Down Expand Up @@ -38,7 +37,7 @@ public function getInstance(): object
* error).
*
* @param string $methodName A called method in the {@see $instance}.
* @param array $arguments A list of arguments passed to a called method. The order must be maintained.
* @param mixed[] $arguments A list of arguments passed to a called method. The order must be maintained.
*
* @throws Throwable In case of error happen during the method call.
*
Expand Down Expand Up @@ -67,7 +66,7 @@ protected function call(string $methodName, array $arguments): mixed
* always returned.
*
* @param string $methodName A called method in the {@see $instance}.
* @param array $arguments A list of arguments passed to a called method. The order must be maintained.
* @param mixed[] $arguments A list of arguments passed to a called method. The order must be maintained.
* @param mixed $result Return value of a called method.
* @param float $timeStart UNIX timestamp right before proxy method call. For example: `1656657586.4849`.
*
Expand All @@ -87,27 +86,24 @@ protected function afterCall(
*
* @param object $instance {@see $instance}.
*
* @return $this A new instance of the same class
* @return static A new instance of the same class
*/
protected function getNewStaticInstance(object $instance): self
{
/**
* @psalm-suppress UnsafeInstantiation Constructor should be consistent to `getNewStaticInstance()`.
*/
// @phpstan-ignore new.static (Constructor should be consistent to `getNewStaticInstance()`)
return new static($instance);
}

/**
* Just calls a method in the {@see $instance}.
*
* @param string $methodName A called method in the {@see $instance}.
* @param array $arguments A list of arguments passed to a called method. The order must be maintained.
* @param mixed[] $arguments A list of arguments passed to a called method. The order must be maintained.
*
* @return mixed Return value of a called method.
*/
private function callInternal(string $methodName, array $arguments): mixed
{
/** @psalm-suppress MixedMethodCall */
return $this->instance->$methodName(...$arguments);
}

Expand Down
18 changes: 5 additions & 13 deletions src/ProxyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
public function __construct(?string $cachePath = null)
{
$this->classCache = ($cachePath !== null && $cachePath !== '') ? new ClassCache($cachePath) : null;

Check warning on line 35 in src/ProxyManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndAllSubExprNegation": @@ @@ */ public function __construct(?string $cachePath = null) { - $this->classCache = $cachePath !== null && $cachePath !== '' ? new ClassCache($cachePath) : null; + $this->classCache = !($cachePath !== null) && !($cachePath !== '') ? new ClassCache($cachePath) : null; $this->classRenderer = new ClassRenderer(); $this->classConfigFactory = new ClassConfigFactory(); }

Check warning on line 35 in src/ProxyManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "NotIdentical": @@ @@ */ public function __construct(?string $cachePath = null) { - $this->classCache = $cachePath !== null && $cachePath !== '' ? new ClassCache($cachePath) : null; + $this->classCache = $cachePath !== null && $cachePath === '' ? new ClassCache($cachePath) : null; $this->classRenderer = new ClassRenderer(); $this->classConfigFactory = new ClassConfigFactory(); }
$this->classRenderer = new ClassRenderer();
$this->classConfigFactory = new ClassConfigFactory();
}
Expand All @@ -40,14 +40,12 @@
/**
* Creates object proxy based on an interface / a class and parent proxy class.
*
* @param string $baseStructure Either or an interface or a class for proxying method calls.
* @param class-string $baseStructure Either or an interface or a class for proxying method calls.
* @param string $parentProxyClass A base proxy class which acts like a parent for dynamically created proxy.
* {@see ObjectProxy} or a class extended from it must be used.
* @param array $proxyConstructorArguments A list of arguments passed to proxy constructor
* @param mixed[] $proxyConstructorArguments A list of arguments passed to proxy constructor
* ({@see ObjectProxy::__construct}).
*
* @psalm-param class-string $baseStructure
*
* @throws Exception In case of error during creation or working with cache / requiring PHP code.
*
* @return ObjectProxy A subclass of {@see ObjectProxy}.
Expand All @@ -58,14 +56,11 @@
array $proxyConstructorArguments,
): ObjectProxy {
$className = $baseStructure . self::PROXY_SUFFIX;
/** @psalm-var class-string $shortClassName */
/** @var class-string $shortClassName */
$shortClassName = self::getProxyClassName($className);

if (class_exists($shortClassName)) {
/**
* @var ObjectProxy
* @psalm-suppress MixedMethodCall
*/
/** @var ObjectProxy */
return new $shortClassName(...$proxyConstructorArguments);
}

Expand All @@ -77,18 +72,15 @@
$this->classCache?->set($baseStructure, $parentProxyClass, $classDeclaration);
}
if (!$this->classCache) {
/** @psalm-suppress UnusedFunctionCall Bug https://github.com/vimeo/psalm/issues/8406 */
eval(str_replace('<?php', '', $classDeclaration));
} else {
$path = $this->classCache->getClassPath($baseStructure, $parentProxyClass);
/** @psalm-suppress UnresolvableInclude */
require $path;
}

/**
* @var ObjectProxy
* @psalm-var class-string $shortClassName
* @psalm-suppress MixedMethodCall
* @var class-string $shortClassName
*/
return new $shortClassName(...$proxyConstructorArguments);
}
Expand Down
Loading