Skip to content

Commit 5d511b5

Browse files
committed
Update the property node API
Changes were asked on the review: #623
1 parent 2edbb6e commit 5d511b5

File tree

11 files changed

+101
-91
lines changed

11 files changed

+101
-91
lines changed

spec/Prophecy/Argument/ArgumentsWildcardSpec.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ function it_wraps_non_token_arguments_into_ExactValueToken(\stdClass $object)
1414
$class = get_class($object->getWrappedObject());
1515
$id = spl_object_id($object->getWrappedObject());
1616

17-
$objHash = "exact(42), exact(\"zet\"), exact($class#$id Object (\n" .
18-
" 'objectProphecyClosureContainer' => Prophecy\Doubler\ObjectProphecyClosureContainer#%s Object (\n" .
19-
" 'closure' => Closure#%s Object (\n" .
20-
" 0 => Closure#%s Object\n" .
21-
" )\n" .
22-
" )\n" .
23-
"))";
17+
$objHash = "exact(42), exact(\"zet\"), exact($class#$id Object (\n"
18+
." 'objectProphecyClosureContainer' => Prophecy\Doubler\ObjectProphecyClosureContainer#%s Object (\n"
19+
." 'closure' => Closure#%s Object (\n"
20+
." 0 => Closure#%s Object\n"
21+
." )\n"
22+
." )\n"
23+
."))";
2424

2525
$idRegexExpr = '[0-9]+';
2626
$this->__toString()->shouldMatch(sprintf('/^%s$/', sprintf(preg_quote("$objHash"), $idRegexExpr, $idRegexExpr, $idRegexExpr)));

spec/Prophecy/Argument/Token/ExactValueTokenSpec.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ function it_generates_proper_string_representation_for_object(\stdClass $object)
136136
$objHash = sprintf('exact(%s#%s',
137137
get_class($object->getWrappedObject()),
138138
spl_object_id($object->getWrappedObject())
139-
) . " Object (\n" .
140-
" 'objectProphecyClosureContainer' => Prophecy\Doubler\ObjectProphecyClosureContainer#%s Object (\n" .
141-
" 'closure' => Closure#%s Object (\n" .
142-
" 0 => Closure#%s Object\n" .
143-
" )\n" .
144-
" )\n" .
145-
"))";
139+
)." Object (\n"
140+
." 'objectProphecyClosureContainer' => Prophecy\Doubler\ObjectProphecyClosureContainer#%s Object (\n"
141+
." 'closure' => Closure#%s Object (\n"
142+
." 0 => Closure#%s Object\n"
143+
." )\n"
144+
." )\n"
145+
."))";
146146

147147
$this->beConstructedWith($object);
148148

spec/Prophecy/Argument/Token/IdenticalValueTokenSpec.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ function it_generates_proper_string_representation_for_object($object)
144144
$objHash = sprintf('identical(%s#%s',
145145
get_class($object->getWrappedObject()),
146146
spl_object_id($object->getWrappedObject())
147-
) . " Object (\n" .
148-
" 'objectProphecyClosureContainer' => Prophecy\Doubler\ObjectProphecyClosureContainer#%s Object (\n" .
149-
" 'closure' => Closure#%s Object (\n" .
150-
" 0 => Closure#%s Object\n" .
151-
" )\n" .
152-
" )\n" .
153-
"))";
147+
)." Object (\n"
148+
." 'objectProphecyClosureContainer' => Prophecy\Doubler\ObjectProphecyClosureContainer#%s Object (\n"
149+
." 'closure' => Closure#%s Object (\n"
150+
." 0 => Closure#%s Object\n"
151+
." )\n"
152+
." )\n"
153+
."))";
154154

155155
$this->beConstructedWith($object);
156156

spec/Prophecy/Doubler/ClassPatch/ProphecySubjectPatchSpec.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Prophecy\Argument;
77
use Prophecy\Doubler\Generator\Node\ClassNode;
88
use Prophecy\Doubler\Generator\Node\MethodNode;
9+
use Prophecy\Doubler\Generator\Node\PropertyNode;
910
use Prophecy\Doubler\Generator\Node\PropertyTypeNode;
1011
use Prophecy\Doubler\Generator\Node\ReturnTypeNode;
1112
use Prophecy\Doubler\Generator\Node\Type\BuiltinType;
@@ -31,9 +32,11 @@ function it_forces_class_to_implement_ProphecySubjectInterface(ClassNode $node)
3132
{
3233
$node->addInterface('Prophecy\Prophecy\ProphecySubjectInterface')->shouldBeCalled();
3334
$node->addProperty(
34-
'objectProphecyClosureContainer',
35-
'private',
36-
new PropertyTypeNode('Prophecy\Doubler\ObjectProphecyClosureContainer')
35+
new PropertyNode(
36+
'objectProphecyClosureContainer',
37+
'private',
38+
new PropertyTypeNode('Prophecy\Doubler\ObjectProphecyClosureContainer')
39+
)
3740
)->willReturn(Argument::type('Prophecy\Doubler\ObjectProphecyClosureContainer'));
3841

3942
$node->getMethods()->willReturn(array());
@@ -54,9 +57,11 @@ function it_forces_all_class_methods_except_constructor_to_proxy_calls_into_prop
5457
) {
5558
$node->addInterface('Prophecy\Prophecy\ProphecySubjectInterface')->willReturn(null);
5659
$node->addProperty(
57-
'objectProphecyClosureContainer',
58-
'private',
59-
new PropertyTypeNode('Prophecy\Doubler\ObjectProphecyClosureContainer')
60+
new PropertyNode(
61+
'objectProphecyClosureContainer',
62+
'private',
63+
new PropertyTypeNode('Prophecy\Doubler\ObjectProphecyClosureContainer')
64+
)
6065
)->willReturn(Argument::type('Prophecy\Doubler\ObjectProphecyClosureContainer'));
6166

6267
$node->hasMethod(Argument::any())->willReturn(false);

spec/Prophecy/Doubler/Generator/Node/ClassNodeSpec.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSpec\ObjectBehavior;
66
use Prophecy\Doubler\Generator\Node\MethodNode;
7+
use Prophecy\Doubler\Generator\Node\PropertyNode;
78
use Prophecy\Exception\Doubler\MethodNotExtendableException;
89

910
class ClassNodeSpec extends ObjectBehavior
@@ -120,7 +121,7 @@ function it_does_not_have_properties_by_default()
120121
$this->getProperties()->shouldHaveCount(0);
121122
}
122123

123-
function it_is_able_to_have_properties()
124+
function it_is_able_to_have_properties_deprecated_api()
124125
{
125126
$this->addProperty('title');
126127
$this->addProperty('text', 'private');
@@ -130,6 +131,15 @@ function it_is_able_to_have_properties()
130131
));
131132
}
132133

134+
function it_is_able_to_have_properties()
135+
{
136+
$prop1 = new PropertyNode('title');
137+
$prop2 = new PropertyNode('text', 'private');
138+
$this->addProperty($prop1);
139+
$this->addProperty($prop2);
140+
$this->getPropertyNodes()->shouldReturn(['title' => $prop1, 'text' => $prop2]);
141+
}
142+
133143
function its_addProperty_does_not_accept_unsupported_visibility()
134144
{
135145
$this->shouldThrow('InvalidArgumentException')->duringAddProperty('title', 'town');

spec/Prophecy/Util/StringUtilSpec.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ function it_generates_proper_string_representation_for_object(\stdClass $object)
7474
$objHash = sprintf('%s#%s',
7575
get_class($object->getWrappedObject()),
7676
spl_object_id($object->getWrappedObject())
77-
) . " Object (\n" .
78-
" 'objectProphecyClosureContainer' => Prophecy\Doubler\ObjectProphecyClosureContainer#%s Object (\n" .
79-
" 'closure' => Closure#%s Object (\n" .
80-
" 0 => Closure#%s Object\n" .
81-
" )\n" .
82-
" )\n" .
83-
")";
77+
)." Object (\n"
78+
." 'objectProphecyClosureContainer' => Prophecy\Doubler\ObjectProphecyClosureContainer#%s Object (\n"
79+
." 'closure' => Closure#%s Object (\n"
80+
." 0 => Closure#%s Object\n"
81+
." )\n"
82+
." )\n"
83+
.")";
8484

8585
$idRegexExpr = '[0-9]+';
8686
$this->stringify($object)->shouldMatch(sprintf('/^%s$/', sprintf(preg_quote("$objHash"), $idRegexExpr, $idRegexExpr, $idRegexExpr)));

src/Prophecy/Doubler/ClassPatch/ProphecySubjectPatch.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Prophecy\Doubler\Generator\Node\ArgumentTypeNode;
1616
use Prophecy\Doubler\Generator\Node\ClassNode;
1717
use Prophecy\Doubler\Generator\Node\MethodNode;
18+
use Prophecy\Doubler\Generator\Node\PropertyNode;
1819
use Prophecy\Doubler\Generator\Node\PropertyTypeNode;
1920
use Prophecy\Doubler\Generator\Node\Type\ObjectType;
2021

@@ -47,16 +48,18 @@ public function apply(ClassNode $node)
4748
{
4849
$node->addInterface('Prophecy\Prophecy\ProphecySubjectInterface');
4950
$node->addProperty(
50-
'objectProphecyClosureContainer',
51-
'private',
52-
new PropertyTypeNode('Prophecy\Doubler\ObjectProphecyClosureContainer')
51+
new PropertyNode(
52+
'objectProphecyClosureContainer',
53+
'private',
54+
new PropertyTypeNode('Prophecy\Doubler\ObjectProphecyClosureContainer')
55+
)
5356
);
5457

5558
foreach ($node->getMethods() as $name => $method) {
5659
if ('__construct' === strtolower($name)) {
5760
$method->setCode(
58-
$method->getCode() .
5961
'$this->objectProphecyClosureContainer = new \Prophecy\Doubler\ObjectProphecyClosureContainer();'
62+
.$method->getCode()
6063
);
6164

6265
continue;

src/Prophecy/Doubler/Generator/ClassCodeGenerator.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function generate($classname, Node\ClassNode $class)
5454
)
5555
);
5656

57-
foreach ((array) $class->getPropertyNodes() as $propertyNode) {
57+
foreach ($class->getPropertyNodes() as $propertyNode) {
5858
$code .= $this->generateProperty($propertyNode)."\n";
5959
}
6060
$code .= "\n";
@@ -69,11 +69,7 @@ public function generate($classname, Node\ClassNode $class)
6969

7070
private function generateProperty(Node\PropertyNode $property): string
7171
{
72-
if (PHP_VERSION_ID >= 70400) {
73-
$type = ($type = $this->generateTypes($property->getTypeNode())) ? $type.' ' : '';
74-
} else {
75-
$type = '';
76-
}
72+
$type = ($type = $this->generateTypes($property->getTypeNode())) ? $type.' ' : '';
7773

7874
$php = sprintf("%s %s%s;",
7975
$property->getVisibility(),

src/Prophecy/Doubler/Generator/Node/ClassNode.php

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,10 @@ class ClassNode
3030
*/
3131
private $interfaces = array();
3232

33-
/**
34-
* @var array<string, string>
35-
*
36-
* @phpstan-var array<string, 'public'|'private'|'protected'>
37-
*/
38-
private $properties = array();
39-
4033
/**
4134
* @var array<string, PropertyNode>
4235
*/
43-
private $propertyNodes = array();
36+
private $properties = [];
4437

4538
/**
4639
* @var list<string>
@@ -111,30 +104,49 @@ public function hasInterface($interface)
111104
* @return array<string, string>
112105
*
113106
* @phpstan-return array<string, 'public'|'private'|'protected'>
107+
*
108+
* @deprecated
114109
*/
115110
public function getProperties()
116111
{
117-
return $this->properties;
112+
trigger_deprecation(
113+
'phpspec/prophecy',
114+
'1.24',
115+
'Use getPropertyNodes() instead. It allows you to retrieve the type as well as visibility.'
116+
);
117+
118+
$propertiesOldFormat = array_map(function ($property) {
119+
return $property->getVisibility();
120+
}, $this->properties);
121+
122+
return $propertiesOldFormat;
118123
}
119124

120125
/**
121126
* @return array<string, PropertyNode>
122127
*/
123-
public function getPropertyNodes()
128+
public function getPropertyNodes(): array
124129
{
125-
return $this->propertyNodes;
130+
return $this->properties;
126131
}
127132

128133
/**
129-
* @param string $name
130-
* @param string $visibility
131-
*
132-
* @return void
133-
*
134134
* @phpstan-param 'public'|'private'|'protected' $visibility
135135
*/
136-
public function addProperty($name, $visibility = 'public', ?PropertyTypeNode $typeNode = null)
136+
public function addProperty(PropertyNode|string $property, string $visibility = 'public'): void
137137
{
138+
if ($property instanceof PropertyNode) {
139+
$this->properties[$property->getName()] = $property;
140+
141+
return;
142+
}
143+
144+
trigger_deprecation(
145+
'phpspec/prophecy',
146+
'1.24',
147+
'The method addProperty() now expects a PropertyNode object instead of a string'
148+
);
149+
138150
$visibility = strtolower($visibility);
139151

140152
if (!\in_array($visibility, array('public', 'private', 'protected'), true)) {
@@ -143,15 +155,10 @@ public function addProperty($name, $visibility = 'public', ?PropertyTypeNode $ty
143155
));
144156
}
145157

146-
$propertyNode = new PropertyNode($name);
158+
$propertyNode = new PropertyNode($property);
147159
$propertyNode->setVisibility($visibility);
148-
if ($typeNode) {
149-
$propertyNode->setTypeNode($typeNode);
150-
}
151-
152-
$this->propertyNodes[$name] = $propertyNode;
153160

154-
$this->properties[$name] = $visibility;
161+
$this->properties[$property] = $propertyNode;
155162
}
156163

157164
/**

src/Prophecy/Doubler/Generator/Node/PropertyNode.php

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,21 @@
1818
*/
1919
class PropertyNode
2020
{
21-
private $name;
21+
private string $name;
2222

23-
/**
24-
* @var string
25-
*
26-
* @phpstan-var 'public'|'private'|'protected'
27-
*/
28-
private $visibility = 'public';
23+
/** @phpstan-var 'public'|'private'|'protected' */
24+
private string $visibility;
2925

30-
/**
31-
* @var PropertyTypeNode
32-
*/
33-
private $typeNode;
26+
private PropertyTypeNode $typeNode;
3427

3528
/**
36-
* @param string $name
29+
* @phpstan-param 'public'|'private'|'protected' $visibility
3730
*/
38-
public function __construct(string $name)
31+
public function __construct(string $name, string $visibility = 'public', PropertyTypeNode $typeNode = new PropertyTypeNode())
3932
{
4033
$this->name = $name;
41-
$this->typeNode = new PropertyTypeNode();
34+
$this->setVisibility($visibility);
35+
$this->typeNode = $typeNode;
4236
}
4337

4438
/**
@@ -60,7 +54,7 @@ public function getTypeNode(): PropertyTypeNode
6054
/**
6155
* @return void
6256
*/
63-
public function setTypeNode(PropertyTypeNode $typeNode)
57+
public function setTypeNode(PropertyTypeNode $typeNode): void
6458
{
6559
$this->typeNode = $typeNode;
6660
}
@@ -82,10 +76,8 @@ public function getVisibility(): string
8276
*
8377
* @phpstan-param 'public'|'private'|'protected' $visibility
8478
*/
85-
public function setVisibility(string $visibility)
79+
public function setVisibility(string $visibility): void
8680
{
87-
$visibility = strtolower($visibility);
88-
8981
if (!\in_array($visibility, array('public', 'private', 'protected'), true)) {
9082
throw new InvalidArgumentException(sprintf(
9183
'`%s` method visibility is not supported.', $visibility

0 commit comments

Comments
 (0)