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

Improve ClassAttributesPass for Dynamic Properties #1244

Merged
merged 14 commits into from
Apr 23, 2023
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"psalm:dry-run": "vendor/bin/psalm --alter --issues=all --dry-run",
"psalm:missing": "vendor/bin/psalm --alter --issues=MissingReturnType",
"psalm:security": "vendor/bin/psalm --taint-analysis",
"psalm:shepherd": "vendor/bin/psalm --shepherd --stats --no-diff --no-cache",
"psalm:shepherd": "vendor/bin/psalm --shepherd --show-info=false --stats --no-diff --no-cache",
"test": [
"@phpunit",
"@psalm"
Expand Down
16 changes: 11 additions & 5 deletions library/Mockery/Generator/DefinedTargetClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@
namespace Mockery\Generator;

use ReflectionAttribute;
use ReflectionClass;

use function array_map;
use function array_unique;

use const PHP_VERSION_ID;

class DefinedTargetClass implements TargetClassInterface
{
private $rfc;
private $name;

public function __construct(\ReflectionClass $rfc, $alias = null)
public function __construct(ReflectionClass $rfc, $alias = null)
{
$this->rfc = $rfc;
$this->name = $alias === null ? $rfc->getName() : $alias;
}

public static function factory($name, $alias = null)
{
return new self(new \ReflectionClass($name), $alias);
return new self(new ReflectionClass($name), $alias);
}

public function getAttributes()
Expand All @@ -44,10 +50,10 @@ public function getAttributes()
return [];
}

return array_map(
static fn (ReflectionAttribute $attribute): string => $attribute->getName(),
return array_unique(['\AllowDynamicProperties', ...array_map(
static fn (ReflectionAttribute $attribute): string => '\\' . $attribute->getName(),
$this->rfc->getAttributes()
);
)]);
}

public function getName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ public function apply($code, MockConfiguration $config)
return $code;
}

/** @var array<string> $attributes */
$attributes = $class->getAttributes();

if (!empty($attributes)) {
$attributes = '#[' . implode(',', $attributes) . ']' . PHP_EOL;

if ($attributes !== []) {
return str_replace(
'class Mock',
$attributes . ' class Mock',
'#[\AllowDynamicProperties]',
'#[' . implode(',', $attributes) . ']',
$code
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ protected function renderParams(Method $method, $config)
if (false !== $param->isDefaultValueAvailable()) {
$defaultValue = $param->getDefaultValue();

if (is_object($defaultValue)){
if (is_object($defaultValue)) {
$prefix = get_class($defaultValue);
if ($isPhp81 && enum_exists($prefix)) {
$prefix = var_export($defaultValue, true);
}
}else{
} else {
$prefix = var_export($defaultValue, true);
}

Expand Down
8 changes: 3 additions & 5 deletions library/Mockery/Generator/UndefinedTargetClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace Mockery\Generator;

use const PHP_VERSION_ID;

class UndefinedTargetClass implements TargetClassInterface
{
private $name;
Expand All @@ -36,11 +38,7 @@ public static function factory($name)

public function getAttributes()
{
if (\PHP_VERSION_ID < 80000) {
return [];
}

return ['\AllowDynamicProperties'];
return [];
}

public function getName()
Expand Down
1 change: 1 addition & 0 deletions library/Mockery/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Mockery\MockInterface;
use Mockery\Reflector;

#[\AllowDynamicProperties]
class Mock implements MockInterface
{
/**
Expand Down
2 changes: 0 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1382,11 +1382,9 @@
<code>apply</code>
</MissingReturnType>
<MixedArgument>
<code>$attributes</code>
<code>$code</code>
</MixedArgument>
<MixedAssignment>
<code>$attributes</code>
<code>$class</code>
</MixedAssignment>
<MixedMethodCall>
Expand Down
2 changes: 1 addition & 1 deletion psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
phpVersion='8.1'
phpVersion='8.2'
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testCanApplyClassAttributes(

$pass = new ClassAttributesPass();

$code = $pass->apply(static::CODE, $config);
$code = $pass->apply(file_get_contents(__FILE__), $config);

self::assertStringContainsString($expected, $code);
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHP81/Php81LanguageFeaturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ReturnTypeWillChange;
use RuntimeException;
use Serializable;

use function pcntl_fork;
use function pcntl_waitpid;
use function pcntl_wexitstatus;
Expand Down