Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 58e733b

Browse files
committed
[PropertyInfo] Made ReflectionExtractor's prefix lists instance variables
This allows for easier, instance-specific overriding of said values.
1 parent 0cfb574 commit 58e733b

File tree

4 files changed

+204
-14
lines changed

4 files changed

+204
-14
lines changed

src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,35 @@ class PhpDocExtractor implements PropertyDescriptionExtractorInterface, Property
5353
*/
5454
private $phpDocTypeHelper;
5555

56-
public function __construct(DocBlockFactoryInterface $docBlockFactory = null)
56+
/**
57+
* @var string[]
58+
*/
59+
private $mutatorPrefixes;
60+
61+
/**
62+
* @var string[]
63+
*/
64+
private $accessorPrefixes;
65+
66+
/**
67+
* @var string[]
68+
*/
69+
private $arrayMutatorPrefixes;
70+
71+
/**
72+
* @param DocBlockFactoryInterface $docBlockFactory
73+
* @param string[]|null $mutatorPrefixes
74+
* @param string[]|null $accessorPrefixes
75+
* @param string[]|null $arrayMutatorPrefixes
76+
*/
77+
public function __construct(DocBlockFactoryInterface $docBlockFactory = null, array $mutatorPrefixes = null, array $accessorPrefixes = null, array $arrayMutatorPrefixes = null)
5778
{
5879
$this->docBlockFactory = $docBlockFactory ?: DocBlockFactory::createInstance();
5980
$this->contextFactory = new ContextFactory();
6081
$this->phpDocTypeHelper = new PhpDocTypeHelper();
82+
$this->mutatorPrefixes = null !== $mutatorPrefixes ? $mutatorPrefixes : ReflectionExtractor::$defaultMutatorPrefixes;
83+
$this->accessorPrefixes = null !== $accessorPrefixes ? $accessorPrefixes : ReflectionExtractor::$defaultAccessorPrefixes;
84+
$this->arrayMutatorPrefixes = null !== $arrayMutatorPrefixes ? $arrayMutatorPrefixes : ReflectionExtractor::$defaultArrayMutatorPrefixes;
6185
}
6286

6387
/**
@@ -137,7 +161,7 @@ public function getTypes($class, $property, array $context = array())
137161
return;
138162
}
139163

140-
if (!in_array($prefix, ReflectionExtractor::$arrayMutatorPrefixes)) {
164+
if (!in_array($prefix, $this->arrayMutatorPrefixes)) {
141165
return $types;
142166
}
143167

@@ -217,7 +241,7 @@ private function getDocBlockFromProperty($class, $property)
217241
*/
218242
private function getDocBlockFromMethod($class, $ucFirstProperty, $type)
219243
{
220-
$prefixes = $type === self::ACCESSOR ? ReflectionExtractor::$accessorPrefixes : ReflectionExtractor::$mutatorPrefixes;
244+
$prefixes = $type === self::ACCESSOR ? $this->accessorPrefixes : $this->mutatorPrefixes;
221245
$prefix = null;
222246

223247
foreach ($prefixes as $prefix) {

src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,53 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
3131
*
3232
* @var string[]
3333
*/
34-
public static $mutatorPrefixes = array('add', 'remove', 'set');
34+
public static $defaultMutatorPrefixes = array('add', 'remove', 'set');
3535

3636
/**
3737
* @internal
3838
*
3939
* @var string[]
4040
*/
41-
public static $accessorPrefixes = array('is', 'can', 'get');
41+
public static $defaultAccessorPrefixes = array('is', 'can', 'get');
4242

4343
/**
4444
* @internal
4545
*
4646
* @var string[]
4747
*/
48-
public static $arrayMutatorPrefixes = array('add', 'remove');
48+
public static $defaultArrayMutatorPrefixes = array('add', 'remove');
4949

50+
/**
51+
* @var bool
52+
*/
5053
private $supportsParameterType;
5154

52-
public function __construct()
55+
/**
56+
* @var string[]
57+
*/
58+
private $mutatorPrefixes;
59+
60+
/**
61+
* @var string[]
62+
*/
63+
private $accessorPrefixes;
64+
65+
/**
66+
* @var string[]
67+
*/
68+
private $arrayMutatorPrefixes;
69+
70+
/**
71+
* @param string[]|null $mutatorPrefixes
72+
* @param string[]|null $accessorPrefixes
73+
* @param string[]|null $arrayMutatorPrefixes
74+
*/
75+
public function __construct(array $mutatorPrefixes = null, array $accessorPrefixes = null, array $arrayMutatorPrefixes = null)
5376
{
5477
$this->supportsParameterType = method_exists('ReflectionParameter', 'getType');
78+
$this->mutatorPrefixes = null !== $mutatorPrefixes ? $mutatorPrefixes : self::$defaultMutatorPrefixes;
79+
$this->accessorPrefixes = null !== $accessorPrefixes ? $accessorPrefixes : self::$defaultAccessorPrefixes;
80+
$this->arrayMutatorPrefixes = null !== $arrayMutatorPrefixes ? $arrayMutatorPrefixes : self::$defaultArrayMutatorPrefixes;
5581
}
5682

5783
/**
@@ -174,7 +200,7 @@ private function extractFromMutator($class, $property)
174200
return;
175201
}
176202

177-
if (in_array($prefix, self::$arrayMutatorPrefixes)) {
203+
if (in_array($prefix, $this->arrayMutatorPrefixes)) {
178204
$type = new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), $type);
179205
}
180206

@@ -266,7 +292,7 @@ private function getAccessorMethod($class, $property)
266292
{
267293
$ucProperty = ucfirst($property);
268294

269-
foreach (self::$accessorPrefixes as $prefix) {
295+
foreach ($this->accessorPrefixes as $prefix) {
270296
try {
271297
$reflectionMethod = new \ReflectionMethod($class, $prefix.$ucProperty);
272298
if ($reflectionMethod->isStatic()) {
@@ -298,9 +324,9 @@ private function getMutatorMethod($class, $property)
298324
$ucProperty = ucfirst($property);
299325
$ucSingulars = (array) Inflector::singularize($ucProperty);
300326

301-
foreach (self::$mutatorPrefixes as $prefix) {
327+
foreach ($this->mutatorPrefixes as $prefix) {
302328
$names = array($ucProperty);
303-
if (in_array($prefix, self::$arrayMutatorPrefixes)) {
329+
if (in_array($prefix, $this->arrayMutatorPrefixes)) {
304330
$names = array_merge($names, $ucSingulars);
305331
}
306332

@@ -332,10 +358,10 @@ private function getMutatorMethod($class, $property)
332358
*/
333359
private function getPropertyName($methodName, array $reflectionProperties)
334360
{
335-
$pattern = implode('|', array_merge(self::$accessorPrefixes, self::$mutatorPrefixes));
361+
$pattern = implode('|', array_merge($this->accessorPrefixes, $this->mutatorPrefixes));
336362

337-
if (preg_match('/^('.$pattern.')(.+)$/i', $methodName, $matches)) {
338-
if (!in_array($matches[1], self::$arrayMutatorPrefixes)) {
363+
if ('' !== $pattern && preg_match('/^('.$pattern.')(.+)$/i', $methodName, $matches)) {
364+
if (!in_array($matches[1], $this->arrayMutatorPrefixes)) {
339365
return $matches[2];
340366
}
341367

src/Symfony/Component/PropertyInfo/Tests/Extractors/PhpDocExtractorTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ public function testExtract($property, array $type = null, $shortDescription, $l
4040
$this->assertSame($longDescription, $this->extractor->getLongDescription('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', $property));
4141
}
4242

43+
/**
44+
* @dataProvider typesWithCustomPrefixesProvider
45+
*/
46+
public function testExtractTypesWithCustomPrefixes($property, array $type = null)
47+
{
48+
$customExtractor = new PhpDocExtractor(null, array('add', 'remove'), array('is', 'can'));
49+
50+
$this->assertEquals($type, $customExtractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', $property));
51+
}
52+
53+
/**
54+
* @dataProvider typesWithNoPrefixesProvider
55+
*/
56+
public function testExtractTypesWithNoPrefixes($property, array $type = null)
57+
{
58+
$noPrefixExtractor = new PhpDocExtractor(null, array(), array(), array());
59+
60+
$this->assertEquals($type, $noPrefixExtractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', $property));
61+
}
62+
4363
public function typesProvider()
4464
{
4565
return array(
@@ -75,6 +95,76 @@ public function typesProvider()
7595
);
7696
}
7797

98+
public function typesWithCustomPrefixesProvider()
99+
{
100+
return array(
101+
array('foo', null, 'Short description.', 'Long description.'),
102+
array('bar', array(new Type(Type::BUILTIN_TYPE_STRING)), 'This is bar', null),
103+
array('baz', array(new Type(Type::BUILTIN_TYPE_INT)), 'Should be used.', null),
104+
array('foo2', array(new Type(Type::BUILTIN_TYPE_FLOAT)), null, null),
105+
array('foo3', array(new Type(Type::BUILTIN_TYPE_CALLABLE)), null, null),
106+
array('foo4', array(new Type(Type::BUILTIN_TYPE_NULL)), null, null),
107+
array('foo5', null, null, null),
108+
array(
109+
'files',
110+
array(
111+
new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, 'SplFileInfo')),
112+
new Type(Type::BUILTIN_TYPE_RESOURCE),
113+
),
114+
null,
115+
null,
116+
),
117+
array('bal', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime')), null, null),
118+
array('parent', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy')), null, null),
119+
array('collection', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime'))), null, null),
120+
array('a', null, 'A.', null),
121+
array('b', null, 'B.', null),
122+
array('c', array(new Type(Type::BUILTIN_TYPE_BOOL, true)), null, null),
123+
array('d', array(new Type(Type::BUILTIN_TYPE_BOOL)), null, null),
124+
array('e', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_RESOURCE))), null, null),
125+
array('f', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime'))), null, null),
126+
array('g', array(new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true)), 'Nullable array.', null),
127+
array('donotexist', null, null, null),
128+
array('staticGetter', null, null, null),
129+
array('staticSetter', null, null, null),
130+
);
131+
}
132+
133+
public function typesWithNoPrefixesProvider()
134+
{
135+
return array(
136+
array('foo', null, 'Short description.', 'Long description.'),
137+
array('bar', array(new Type(Type::BUILTIN_TYPE_STRING)), 'This is bar', null),
138+
array('baz', array(new Type(Type::BUILTIN_TYPE_INT)), 'Should be used.', null),
139+
array('foo2', array(new Type(Type::BUILTIN_TYPE_FLOAT)), null, null),
140+
array('foo3', array(new Type(Type::BUILTIN_TYPE_CALLABLE)), null, null),
141+
array('foo4', array(new Type(Type::BUILTIN_TYPE_NULL)), null, null),
142+
array('foo5', null, null, null),
143+
array(
144+
'files',
145+
array(
146+
new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, 'SplFileInfo')),
147+
new Type(Type::BUILTIN_TYPE_RESOURCE),
148+
),
149+
null,
150+
null,
151+
),
152+
array('bal', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime')), null, null),
153+
array('parent', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy')), null, null),
154+
array('collection', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime'))), null, null),
155+
array('a', null, 'A.', null),
156+
array('b', null, 'B.', null),
157+
array('c', null, null, null),
158+
array('d', null, null, null),
159+
array('e', null, null, null),
160+
array('f', null, null, null),
161+
array('g', array(new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true)), 'Nullable array.', null),
162+
array('donotexist', null, null, null),
163+
array('staticGetter', null, null, null),
164+
array('staticSetter', null, null, null),
165+
);
166+
}
167+
78168
public function testReturnNullOnEmptyDocBlock()
79169
{
80170
$this->assertNull($this->extractor->getShortDescription(EmptyDocBlock::class, 'foo'));

src/Symfony/Component/PropertyInfo/Tests/Extractors/ReflectionExtractorTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,56 @@ public function testGetProperties()
6060
);
6161
}
6262

63+
public function testGetPropertiesWithCustomPrefixes()
64+
{
65+
$customExtractor = new ReflectionExtractor(array('add', 'remove'), array('is', 'can'));
66+
67+
$this->assertSame(
68+
array(
69+
'bal',
70+
'parent',
71+
'collection',
72+
'B',
73+
'Guid',
74+
'g',
75+
'foo',
76+
'foo2',
77+
'foo3',
78+
'foo4',
79+
'foo5',
80+
'files',
81+
'c',
82+
'd',
83+
'e',
84+
'f',
85+
),
86+
$customExtractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy')
87+
);
88+
}
89+
90+
public function testGetPropertiesWithNoPrefixes()
91+
{
92+
$noPrefixExtractor = new ReflectionExtractor(array(), array(), array());
93+
94+
$this->assertSame(
95+
array(
96+
'bal',
97+
'parent',
98+
'collection',
99+
'B',
100+
'Guid',
101+
'g',
102+
'foo',
103+
'foo2',
104+
'foo3',
105+
'foo4',
106+
'foo5',
107+
'files',
108+
),
109+
$noPrefixExtractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy')
110+
);
111+
}
112+
63113
/**
64114
* @dataProvider typesProvider
65115
*/

0 commit comments

Comments
 (0)