Skip to content

[DoctrineBridge] add parameter type declarations where possible #32769

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 1 commit into from
Jul 27, 2019
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 @@ -44,7 +44,7 @@ public function isOptional()
/**
* {@inheritdoc}
*/
public function warmUp($cacheDir)
public function warmUp(string $cacheDir)
{
foreach ($this->registry->getManagers() as $em) {
// we need the directory no matter the proxy cache generation strategy
Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Bridge/Doctrine/ContainerAwareEventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ public function removeEventListener($events, $listener)
}
}

/**
* @param string $eventName
*/
private function initializeListeners($eventName)
private function initializeListeners(string $eventName)
{
foreach ($this->listeners[$eventName] as $hash => $listener) {
if (\is_string($listener)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ public function __construct(ManagerRegistry $registry)

/**
* Adds the stack logger for a connection.
*
* @param string $name
* @param DebugStack $logger
*/
public function addLogger($name, DebugStack $logger)
public function addLogger(string $name, DebugStack $logger)
{
$this->loggers[$name] = $logger;
}
Expand Down Expand Up @@ -120,7 +117,7 @@ public function getName()
return 'db';
}

private function sanitizeQueries($connectionName, $queries)
private function sanitizeQueries(string $connectionName, array $queries)
{
foreach ($queries as $i => $query) {
$queries[$i] = $this->sanitizeQuery($connectionName, $query);
Expand All @@ -129,7 +126,7 @@ private function sanitizeQueries($connectionName, $queries)
return $queries;
}

private function sanitizeQuery($connectionName, $query)
private function sanitizeQuery(string $connectionName, $query)
{
$query['explainable'] = true;
if (null === $query['params']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,8 @@ protected function loadMappingInformation(array $objectManager, ContainerBuilder
* Register the alias for this mapping driver.
*
* Aliases can be used in the Query languages of all the Doctrine object managers to simplify writing tasks.
*
* @param array $mappingConfig
* @param string $mappingName
*/
protected function setMappingDriverAlias($mappingConfig, $mappingName)
protected function setMappingDriverAlias(array $mappingConfig, string $mappingName)
{
if (isset($mappingConfig['alias'])) {
$this->aliasMap[$mappingConfig['alias']] = $mappingConfig['prefix'];
Expand All @@ -117,12 +114,9 @@ protected function setMappingDriverAlias($mappingConfig, $mappingName)
/**
* Register the mapping driver configuration for later use with the object managers metadata driver chain.
*
* @param array $mappingConfig
* @param string $mappingName
*
* @throws \InvalidArgumentException
*/
protected function setMappingDriverConfig(array $mappingConfig, $mappingName)
protected function setMappingDriverConfig(array $mappingConfig, string $mappingName)
{
$mappingDirectory = $mappingConfig['dir'];
if (!is_dir($mappingDirectory)) {
Expand Down Expand Up @@ -171,11 +165,8 @@ protected function getMappingDriverBundleConfigDefaults(array $bundleConfig, \Re

/**
* Register all the collected mapping information with the object manager by registering the appropriate mapping drivers.
*
* @param array $objectManager
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function registerMappingDrivers($objectManager, ContainerBuilder $container)
protected function registerMappingDrivers(array $objectManager, ContainerBuilder $container)
{
// configure metadata driver for each bundle based on the type of mapping files found
if ($container->hasDefinition($this->getObjectManagerElementName($objectManager['name'].'_metadata_driver'))) {
Expand Down Expand Up @@ -225,12 +216,9 @@ protected function registerMappingDrivers($objectManager, ContainerBuilder $cont
/**
* Assertion if the specified mapping information is valid.
*
* @param array $mappingConfig
* @param string $objectManagerName
*
* @throws \InvalidArgumentException
*/
protected function assertValidMappingConfiguration(array $mappingConfig, $objectManagerName)
protected function assertValidMappingConfiguration(array $mappingConfig, string $objectManagerName)
{
if (!$mappingConfig['type'] || !$mappingConfig['dir'] || !$mappingConfig['prefix']) {
throw new \InvalidArgumentException(sprintf('Mapping definitions for Doctrine manager "%s" require at least the "type", "dir" and "prefix" options.', $objectManagerName));
Expand All @@ -252,12 +240,9 @@ protected function assertValidMappingConfiguration(array $mappingConfig, $object
/**
* Detects what metadata driver to use for the supplied directory.
*
* @param string $dir A directory path
* @param ContainerBuilder $container A ContainerBuilder instance
*
* @return string|null A metadata driver short name, if one can be detected
*/
protected function detectMetadataDriver($dir, ContainerBuilder $container)
protected function detectMetadataDriver(string $dir, ContainerBuilder $container)
{
$configPath = $this->getMappingResourceConfigDirectory();
$extension = $this->getMappingResourceExtension();
Expand Down Expand Up @@ -286,30 +271,21 @@ protected function detectMetadataDriver($dir, ContainerBuilder $container)
/**
* Loads a configured object manager metadata, query or result cache driver.
*
* @param array $objectManager A configured object manager
* @param ContainerBuilder $container A ContainerBuilder instance
* @param string $cacheName
*
* @throws \InvalidArgumentException in case of unknown driver type
*/
protected function loadObjectManagerCacheDriver(array $objectManager, ContainerBuilder $container, $cacheName)
protected function loadObjectManagerCacheDriver(array $objectManager, ContainerBuilder $container, string $cacheName)
{
$this->loadCacheDriver($cacheName, $objectManager['name'], $objectManager[$cacheName.'_driver'], $container);
}

/**
* Loads a cache driver.
*
* @param string $cacheName The cache driver name
* @param string $objectManagerName The object manager name
* @param array $cacheDriver The cache driver mapping
* @param ContainerBuilder $container The ContainerBuilder instance
*
* @return string
*
* @throws \InvalidArgumentException
*/
protected function loadCacheDriver($cacheName, $objectManagerName, array $cacheDriver, ContainerBuilder $container)
protected function loadCacheDriver(string $cacheName, string $objectManagerName, array $cacheDriver, ContainerBuilder $container)
{
$cacheDriverServiceId = $this->getObjectManagerElementName($objectManagerName.'_'.$cacheName);

Expand Down Expand Up @@ -412,11 +388,9 @@ protected function fixManagersAutoMappings(array $managerConfigs, array $bundles
*
* @example $name is 'entity_manager' then the result would be 'doctrine.orm.entity_manager'
*
* @param string $name
*
* @return string
*/
abstract protected function getObjectManagerElementName($name);
abstract protected function getObjectManagerElementName(string $name);

/**
* Noun that describes the mapped objects such as Entity or Document.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private function addTaggedListeners(ContainerBuilder $container)
}
}

private function getEventManagerDef(ContainerBuilder $container, $name)
private function getEventManagerDef(ContainerBuilder $container, string $name)
{
if (!isset($this->eventManagers[$name])) {
$this->eventManagers[$name] = $container->getDefinition(sprintf($this->managerTemplate, $name));
Expand All @@ -128,12 +128,9 @@ private function getEventManagerDef(ContainerBuilder $container, $name)
* @see https://bugs.php.net/bug.php?id=53710
* @see https://bugs.php.net/bug.php?id=60926
*
* @param string $tagName
* @param ContainerBuilder $container
*
* @return array
*/
private function findAndSortTags($tagName, ContainerBuilder $container)
private function findAndSortTags(string $tagName, ContainerBuilder $container)
{
$sortedTags = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(string $key, string $providerId)
$this->providerId = $providerId;
}

public function create(ContainerBuilder $container, $id, $config)
public function create(ContainerBuilder $container, string $id, array $config)
{
$container
->setDefinition($id, new ChildDefinition($this->providerId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct(ObjectManager $manager, string $class, IdReader $idR
/**
* {@inheritdoc}
*/
public function loadChoiceList($value = null)
public function loadChoiceList(callable $value = null)
{
if ($this->choiceList) {
return $this->choiceList;
Expand All @@ -78,7 +78,7 @@ public function loadChoiceList($value = null)
/**
* {@inheritdoc}
*/
public function loadValuesForChoices(array $choices, $value = null)
public function loadValuesForChoices(array $choices, callable $value = null)
{
// Performance optimization
if (empty($choices)) {
Expand Down Expand Up @@ -110,7 +110,7 @@ public function loadValuesForChoices(array $choices, $value = null)
/**
* {@inheritdoc}
*/
public function loadChoicesForValues(array $values, $value = null)
public function loadChoicesForValues(array $values, callable $value = null)
{
// Performance optimization
// Also prevents the generation of "WHERE id IN ()" queries through the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ public function getEntities();
/**
* Returns an array of entities matching the given identifiers.
*
* @param string $identifier The identifier field of the object. This method
* is not applicable for fields with multiple
* identifiers.
* @param array $values The values of the identifiers
*
* @return array The entities
*/
public function getEntitiesByIds($identifier, array $values);
public function getEntitiesByIds(string $identifier, array $values);
}
4 changes: 1 addition & 3 deletions src/Symfony/Bridge/Doctrine/Form/ChoiceList/IdReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,9 @@ public function isIntId(): bool
*
* This method assumes that the object has a single-column ID.
*
* @param object $object The object
*
* @return mixed The ID value
*/
public function getIdValue($object)
public function getIdValue(object $object = null)
{
if (!$object) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getEntities()
/**
* {@inheritdoc}
*/
public function getEntitiesByIds($identifier, array $values)
public function getEntitiesByIds(string $identifier, array $values)
{
$qb = clone $this->queryBuilder;
$alias = current($qb->getRootAliases());
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(ManagerRegistry $registry)
/**
* {@inheritdoc}
*/
public function guessType($class, $property)
public function guessType(string $class, string $property)
{
if (!$ret = $this->getMetadata($class)) {
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::LOW_CONFIDENCE);
Expand Down Expand Up @@ -94,7 +94,7 @@ public function guessType($class, $property)
/**
* {@inheritdoc}
*/
public function guessRequired($class, $property)
public function guessRequired(string $class, string $property)
{
$classMetadatas = $this->getMetadata($class);

Expand Down Expand Up @@ -132,7 +132,7 @@ public function guessRequired($class, $property)
/**
* {@inheritdoc}
*/
public function guessMaxLength($class, $property)
public function guessMaxLength(string $class, string $property)
{
$ret = $this->getMetadata($class);
if ($ret && isset($ret[0]->fieldMappings[$property]) && !$ret[0]->hasAssociation($property)) {
Expand All @@ -151,7 +151,7 @@ public function guessMaxLength($class, $property)
/**
* {@inheritdoc}
*/
public function guessPattern($class, $property)
public function guessPattern(string $class, string $property)
{
$ret = $this->getMetadata($class);
if ($ret && isset($ret[0]->fieldMappings[$property]) && !$ret[0]->hasAssociation($property)) {
Expand All @@ -161,7 +161,7 @@ public function guessPattern($class, $property)
}
}

protected function getMetadata($class)
protected function getMetadata(string $class)
{
// normalize class name
$class = self::getRealClass(ltrim($class, '\\'));
Expand Down
24 changes: 8 additions & 16 deletions src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\ChoiceList\DoctrineChoiceLoader;
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
use Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader;
Expand Down Expand Up @@ -49,14 +50,12 @@ abstract class DoctrineType extends AbstractType implements ResetInterface
*
* For backwards compatibility, objects are cast to strings by default.
*
* @param object $choice The object
*
* @return string The string representation of the object
*
* @internal This method is public to be usable as callback. It should not
* be used in user code.
*/
public static function createChoiceLabel($choice)
public static function createChoiceLabel(object $choice)
{
return (string) $choice;
}
Expand All @@ -68,17 +67,16 @@ public static function createChoiceLabel($choice)
* a single-column integer ID. In that case, the value of the field is
* the ID of the object. That ID is also used as field name.
*
* @param object $choice The object
* @param int|string $key The choice key
* @param string $value The choice value. Corresponds to the object's
* ID here.
* @param int|string $key The choice key
* @param string $value The choice value. Corresponds to the object's
* ID here.
*
* @return string The field name
*
* @internal This method is public to be usable as callback. It should not
* be used in user code.
*/
public static function createChoiceName($choice, $key, $value)
public static function createChoiceName(object $choice, $key, string $value)
{
return str_replace('-', '_', (string) $value);
}
Expand All @@ -88,15 +86,13 @@ public static function createChoiceName($choice, $key, $value)
* For instance in ORM two query builders with an equal SQL string and
* equal parameters are considered to be equal.
*
* @param object $queryBuilder
*
* @return array|false Array with important QueryBuilder parts or false if
* they can't be determined
*
* @internal This method is public to be usable as callback. It should not
* be used in user code.
*/
public function getQueryBuilderPartsForCachingHash($queryBuilder)
public function getQueryBuilderPartsForCachingHash(QueryBuilder $queryBuilder)
{
return false;
}
Expand Down Expand Up @@ -265,13 +261,9 @@ public function configureOptions(OptionsResolver $resolver)
/**
* Return the default loader object.
*
* @param ObjectManager $manager
* @param mixed $queryBuilder
* @param string $class
*
* @return EntityLoaderInterface
*/
abstract public function getLoader(ObjectManager $manager, $queryBuilder, $class);
abstract public function getLoader(ObjectManager $manager, QueryBuilder $queryBuilder, string $class);

public function getParent()
{
Expand Down
Loading