Skip to content

Commit

Permalink
update CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm committed Oct 8, 2024
1 parent ca8c4c2 commit 4488900
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 108 deletions.
10 changes: 5 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ parameters:
count: 1
path: src/DependencyInjection/CompilerPass/ProxyServiceWithMockPass.php

-
message: "#^Parameter \\#1 \\$class of method Symfony\\\\Component\\\\DependencyInjection\\\\Definition\\:\\:setClass\\(\\) expects string\\|null, false given\\.$#"
count: 1
path: src/DependencyInjection/CompilerPass/ProxyServiceWithMockPass.php

-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:children\\(\\)\\.$#"
count: 1
Expand All @@ -25,3 +20,8 @@ parameters:
count: 1
path: src/ServiceMock.php

-
message: "#^Parameter \\#1 \\$initializer of method ProxyManager\\\\Proxy\\\\LazyLoadingInterface\\<object\\>\\:\\:setProxyInitializer\\(\\) expects \\(Closure\\(object\\|null\\=, ProxyManager\\\\Proxy\\\\LazyLoadingInterface\\<object\\>\\=, string\\=, array\\<string, mixed\\>\\=, Closure\\|null\\=, array\\<string, mixed\\>\\=\\)\\: bool\\)\\|null, Closure\\(mixed, ProxyManager\\\\Proxy\\\\LazyLoadingInterface, mixed, array, mixed\\)\\: true given\\.$#"
count: 1
path: src/ServiceMock.php

65 changes: 8 additions & 57 deletions psalm.baseline.xml
Original file line number Diff line number Diff line change
@@ -1,62 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.22.0@fc2c6ab4d5fa5d644d8617089f012f3bb84b8703">
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
<file src="src/DependencyInjection/Configuration.php">
<UndefinedMethod>
<code><![CDATA[children]]></code>
</UndefinedMethod>
</file>
<file src="src/ServiceMock.php">
<UndefinedInterfaceMethod occurrences="1">
<code>getWrappedValueHolderValue</code>
<UndefinedInterfaceMethod>
<code><![CDATA[getWrappedValueHolderValue]]></code>
</UndefinedInterfaceMethod>
</file>
<file src="vendor/laminas/laminas-code/src/Generator/ClassGenerator.php">
<ParseError occurrences="6">
<code>$interface</code>
<code>$interface</code>
<code>)</code>
<code>)</code>
<code>fn</code>
<code>fn</code>
</ParseError>
</file>
<file src="vendor/laminas/laminas-code/src/Generator/FileGenerator.php">
<ParseError occurrences="4">
<code>,</code>
<code>,</code>
<code>=&gt;</code>
<code>fn</code>
</ParseError>
</file>
<file src="vendor/laminas/laminas-code/src/Generator/MethodGenerator.php">
<ParseError occurrences="3">
<code>$item1</code>
<code>)</code>
<code>fn</code>
</ParseError>
</file>
<file src="vendor/laminas/laminas-code/src/Generator/TypeGenerator.php">
<ParseError occurrences="15">
<code>$left</code>
<code>$type</code>
<code>$type</code>
<code>$type</code>
<code>)</code>
<code>)</code>
<code>)</code>
<code>)</code>
<code>,</code>
<code>,</code>
<code>,</code>
<code>fn</code>
<code>fn</code>
<code>fn</code>
<code>fn</code>
</ParseError>
</file>
<file src="vendor/laminas/laminas-code/src/Generator/TypeGenerator/AtomicType.php">
<ParseError occurrences="6">
<code>$type</code>
<code>$type</code>
<code>)</code>
<code>)</code>
<code>fn</code>
<code>fn</code>
</ParseError>
</file>
</files>
3 changes: 0 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('happyr_service_mocking');
Expand Down
3 changes: 0 additions & 3 deletions src/DependencyInjection/HappyrServiceMockingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

class HappyrServiceMockingExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
Expand Down
29 changes: 11 additions & 18 deletions src/Generator/Constructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@

namespace Happyr\ServiceMocking\Generator;

use function array_filter;
use function array_map;
use function implode;
use Laminas\Code\Generator\Exception\InvalidArgumentException;
use Laminas\Code\Generator\PropertyGenerator;
use Laminas\Code\Reflection\MethodReflection;
use Laminas\Code\Reflection\ParameterReflection;
use ProxyManager\Generator\MethodGenerator;
use ProxyManager\ProxyGenerator\Util\Properties;
use ProxyManager\ProxyGenerator\Util\UnsetPropertiesGenerator;
use ReflectionClass;
use ReflectionMethod;
use function reset;
use function var_export;

/**
* The `__construct` implementation for lazy loading proxies.
Expand All @@ -29,7 +22,7 @@ class Constructor extends MethodGenerator
/**
* @throws InvalidArgumentException
*/
public static function generateMethod(ReflectionClass $originalClass, PropertyGenerator $valueHolder): self
public static function generateMethod(\ReflectionClass $originalClass, PropertyGenerator $valueHolder): self
{
$originalConstructor = self::getConstructor($originalClass);

Expand All @@ -41,7 +34,7 @@ public static function generateMethod(ReflectionClass $originalClass, PropertyGe
'static $reflection;'."\n\n"
.'if (! $this->'.$valueHolder->getName().') {'."\n"
.' $reflection = $reflection ?? new \ReflectionClass('
.var_export($originalClass->getName(), true)
.\var_export($originalClass->getName(), true)
.");\n"
.' $this->'.$valueHolder->getName().' = $reflection->newInstanceWithoutConstructor();'."\n"
.UnsetPropertiesGenerator::generateSnippet(Properties::fromReflectionClass($originalClass), 'this')
Expand All @@ -56,13 +49,13 @@ public static function generateMethod(ReflectionClass $originalClass, PropertyGe

private static function generateOriginalConstructorCall(
MethodReflection $originalConstructor,
PropertyGenerator $valueHolder
PropertyGenerator $valueHolder,
): string {
return "\n\n"
.'$this->'.$valueHolder->getName().'->'.$originalConstructor->getName().'('
.implode(
.\implode(
', ',
array_map(
\array_map(
static function (ParameterReflection $parameter): string {
return ($parameter->isVariadic() ? '...' : '').'$'.$parameter->getName();
},
Expand All @@ -72,23 +65,23 @@ static function (ParameterReflection $parameter): string {
.');';
}

private static function getConstructor(ReflectionClass $class): ?MethodReflection
private static function getConstructor(\ReflectionClass $class): ?MethodReflection
{
$constructors = array_map(
static function (ReflectionMethod $method): MethodReflection {
$constructors = \array_map(
static function (\ReflectionMethod $method): MethodReflection {
return new MethodReflection(
$method->getDeclaringClass()->getName(),
$method->getName()
);
},
array_filter(
\array_filter(
$class->getMethods(),
static function (ReflectionMethod $method): bool {
static function (\ReflectionMethod $method): bool {
return $method->isConstructor();
}
)
);

return reset($constructors) ?: null;
return \reset($constructors) ?: null;
}
}
33 changes: 11 additions & 22 deletions src/Generator/LazyLoadingValueHolderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

namespace Happyr\ServiceMocking\Generator;

use function array_map;
use function array_merge;
use function func_get_arg;
use function func_num_args;
use InvalidArgumentException;
use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Generator\MethodGenerator;
use Laminas\Code\Reflection\MethodReflection;
Expand Down Expand Up @@ -37,13 +32,9 @@
use ProxyManager\ProxyGenerator\Util\Properties;
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\GetWrappedValueHolderValue;
use ReflectionClass;
use ReflectionMethod;
use function str_replace;
use function substr;

/**
* Generator for proxies implementing {@see \ProxyManager\Proxy\VirtualProxyInterface}.
* Generator for proxies implementing {@see VirtualProxyInterface}.
*
* This is a 99% copy of ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator.
* This class generates a different constructor.
Expand All @@ -55,19 +46,17 @@
class LazyLoadingValueHolderGenerator implements ProxyGeneratorInterface
{
/**
* {@inheritDoc}
*
* @return void
*
* @throws InvalidProxiedClassException
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*
* @psalm-param array{skipDestructor?: bool, fluentSafe?: bool} $proxyOptions
*/
public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator/* , array $proxyOptions = [] */)
public function generate(\ReflectionClass $originalClass, ClassGenerator $classGenerator/* , array $proxyOptions = [] */)
{
/** @psalm-var array{skipDestructor?: bool, fluentSafe?: bool} $proxyOptions */
$proxyOptions = func_num_args() >= 3 ? func_get_arg(2) : [];
$proxyOptions = \func_num_args() >= 3 ? \func_get_arg(2) : [];

CanProxyAssertion::assertClassCanBeProxied($originalClass);

Expand All @@ -92,12 +81,12 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe
$excludedMethods[] = '__destruct';
}

array_map(
\array_map(
static function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator): void {
ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod);
},
array_merge(
array_map(
\array_merge(
\array_map(
$this->buildLazyLoadingMethodInterceptor($initializer, $valueHolder, $proxyOptions['fluentSafe'] ?? false),
ProxiedMethodsFilter::getProxiedMethods($originalClass, $excludedMethods)
),
Expand Down Expand Up @@ -126,9 +115,9 @@ static function (MethodGenerator $generatedMethod) use ($originalClass, $classGe
private function buildLazyLoadingMethodInterceptor(
InitializerProperty $initializer,
ValueHolderProperty $valueHolder,
bool $fluentSafe
bool $fluentSafe,
): callable {
return static function (ReflectionMethod $method) use ($initializer, $valueHolder, $fluentSafe): LazyLoadingMethodInterceptor {
return static function (\ReflectionMethod $method) use ($initializer, $valueHolder, $fluentSafe): LazyLoadingMethodInterceptor {
$byRef = $method->returnsReference() ? '& ' : '';
$method = LazyLoadingMethodInterceptor::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
Expand All @@ -139,11 +128,11 @@ private function buildLazyLoadingMethodInterceptor(
if ($fluentSafe) {
$valueHolderName = '$this->'.$valueHolder->getName();
$body = $method->getBody();
$newBody = str_replace('return '.$valueHolderName, 'if ('.$valueHolderName.' === $returnValue = '.$byRef.$valueHolderName, $body);
$newBody = \str_replace('return '.$valueHolderName, 'if ('.$valueHolderName.' === $returnValue = '.$byRef.$valueHolderName, $body);

if ($newBody !== $body) {
$method->setBody(
substr($newBody, 0, -1).') {'."\n"
\substr($newBody, 0, -1).') {'."\n"
.' return $this;'."\n"
.'}'."\n\n"
.'return $returnValue;'
Expand Down
1 change: 1 addition & 0 deletions src/Test/RestoreServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ trait RestoreServiceContainer
{
/**
* @internal
*
* @after
*/
public static function _restoreContainer(): void
Expand Down

0 comments on commit 4488900

Please sign in to comment.