Skip to content

[PropertyInfo] Fix dock block lookup fallback loop #27837

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

Merged
merged 3 commits into from
Jul 7, 2018
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function configure()
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
AppBundle\Entity\User: bcrypt
App\Entity\User: bcrypt
</comment>

If you execute the command non-interactively, the first available configured
Expand All @@ -87,16 +87,16 @@ protected function configure()
Pass the full user class path as the second argument to encode passwords for
your own entities:

<info>php %command.full_name% --no-interaction [password] AppBundle\Entity\User</info>
<info>php %command.full_name% --no-interaction [password] App\Entity\User</info>

Executing the command interactively allows you to generate a random salt for
encoding the password:

<info>php %command.full_name% [password] AppBundle\Entity\User</info>
<info>php %command.full_name% [password] App\Entity\User</info>

In case your encoder doesn't require a salt, add the <comment>empty-salt</comment> option:

<info>php %command.full_name% --empty-salt [password] AppBundle\Entity\User</info>
<info>php %command.full_name% --empty-salt [password] App\Entity\User</info>

EOF
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ private function addEncodersSection(ArrayNodeDefinition $rootNode)
->children()
->arrayNode('encoders')
->example(array(
'AppBundle\Entity\User1' => 'bcrypt',
'AppBundle\Entity\User2' => array(
'App\Entity\User1' => 'bcrypt',
'App\Entity\User2' => array(
'algorithm' => 'bcrypt',
'cost' => 13,
),
Expand Down
40 changes: 22 additions & 18 deletions src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,25 +169,21 @@ private function getDocBlock($class, $property)

$ucFirstProperty = ucfirst($property);

try {
switch (true) {
case $docBlock = $this->getDocBlockFromProperty($class, $property):
$data = array($docBlock, self::PROPERTY, null);
break;
switch (true) {
case $docBlock = $this->getDocBlockFromProperty($class, $property):
$data = array($docBlock, self::PROPERTY, null);
break;

case list($docBlock) = $this->getDocBlockFromMethod($class, $ucFirstProperty, self::ACCESSOR):
$data = array($docBlock, self::ACCESSOR, null);
break;
case list($docBlock) = $this->getDocBlockFromMethod($class, $ucFirstProperty, self::ACCESSOR):
$data = array($docBlock, self::ACCESSOR, null);
break;

case list($docBlock, $prefix) = $this->getDocBlockFromMethod($class, $ucFirstProperty, self::MUTATOR):
$data = array($docBlock, self::MUTATOR, $prefix);
break;
case list($docBlock, $prefix) = $this->getDocBlockFromMethod($class, $ucFirstProperty, self::MUTATOR):
$data = array($docBlock, self::MUTATOR, $prefix);
break;

default:
$data = array(null, null, null);
}
} catch (\InvalidArgumentException $e) {
$data = array(null, null, null);
default:
$data = array(null, null, null);
}

return $this->docBlocks[$propertyHash] = $data;
Expand All @@ -210,7 +206,11 @@ private function getDocBlockFromProperty($class, $property)
return;
}

return $this->docBlockFactory->create($reflectionProperty, $this->contextFactory->createFromReflector($reflectionProperty->getDeclaringClass()));
try {
return $this->docBlockFactory->create($reflectionProperty, $this->contextFactory->createFromReflector($reflectionProperty->getDeclaringClass()));
} catch (\InvalidArgumentException $e) {
return null;
}
}

/**
Expand Down Expand Up @@ -251,6 +251,10 @@ private function getDocBlockFromMethod($class, $ucFirstProperty, $type)
return;
}

return array($this->docBlockFactory->create($reflectionMethod, $this->contextFactory->createFromReflector($reflectionMethod)), $prefix);
try {
return array($this->docBlockFactory->create($reflectionMethod, $this->contextFactory->createFromReflector($reflectionMethod)), $prefix);
} catch (\InvalidArgumentException $e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,29 @@ public function testReturnNullOnEmptyDocBlock()
{
$this->assertNull($this->extractor->getShortDescription(EmptyDocBlock::class, 'foo'));
}

public function dockBlockFallbackTypesProvider()
{
return array(
'pub' => array(
'pub', array(new Type(Type::BUILTIN_TYPE_STRING)),
),
'protAcc' => array(
'protAcc', array(new Type(Type::BUILTIN_TYPE_INT)),
),
'protMut' => array(
'protMut', array(new Type(Type::BUILTIN_TYPE_BOOL)),
),
);
}

/**
* @dataProvider dockBlockFallbackTypesProvider
*/
public function testDocBlockFallback($property, $types)
{
$this->assertEquals($types, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\DockBlockFallback', $property));
}
}

class EmptyDocBlock
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Symfony\Component\PropertyInfo\Tests\Fixtures;

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyInfo\Tests\Fixtures;

/**
* PhpDocExtractor should fallback from property -> accessor -> mutator when looking up dockblocks.
*
* @author Martin Rademacher <mano@radebatz.net>
*/
class DockBlockFallback
{
/** @var string $pub */
public $pub = 'pub';

protected $protAcc;
protected $protMut;

public function getPub()
{
return $this->pub;
}

public function setPub($pub)
{
$this->pub = $pub;
}

/**
* @return int
*/
public function getProtAcc()
{
return $this->protAcc;
}

public function setProt($protAcc)
{
$this->protAcc = $protAcc;
}

public function getProtMut()
{
return $this->protMut;
}

/**
* @param bool $protMut
*/
public function setProtMut($protMut)
{
$this->protMut = $protMut;
}
}