Skip to content

Commit

Permalink
Merge pull request #1711 from carusogabriel/null-coalesce-operator
Browse files Browse the repository at this point in the history
Use Null Coalesce Operator
  • Loading branch information
alcaeus committed Jan 4, 2018
2 parents 8a5bc1e + 0e4a074 commit 66dea15
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 101 deletions.
50 changes: 15 additions & 35 deletions lib/Doctrine/ODM/MongoDB/Configuration.php
Expand Up @@ -155,8 +155,7 @@ public function newDefaultAnnotationDriver($paths = array())
*/
public function getMetadataDriverImpl()
{
return isset($this->attributes['metadataDriverImpl']) ?
$this->attributes['metadataDriverImpl'] : null;
return $this->attributes['metadataDriverImpl'] ?? null;
}

/**
Expand All @@ -166,8 +165,7 @@ public function getMetadataDriverImpl()
*/
public function getMetadataCacheImpl()
{
return isset($this->attributes['metadataCacheImpl']) ?
$this->attributes['metadataCacheImpl'] : null;
return $this->attributes['metadataCacheImpl'] ?? null;
}

/**
Expand Down Expand Up @@ -197,8 +195,7 @@ public function setProxyDir($dir)
*/
public function getProxyDir()
{
return isset($this->attributes['proxyDir']) ?
$this->attributes['proxyDir'] : null;
return $this->attributes['proxyDir'] ?? null;
}

/**
Expand All @@ -209,8 +206,7 @@ public function getProxyDir()
*/
public function getAutoGenerateProxyClasses()
{
return isset($this->attributes['autoGenerateProxyClasses']) ?
$this->attributes['autoGenerateProxyClasses'] : true;
return $this->attributes['autoGenerateProxyClasses'] ?? true;
}

/**
Expand All @@ -231,8 +227,7 @@ public function setAutoGenerateProxyClasses($bool)
*/
public function getProxyNamespace()
{
return isset($this->attributes['proxyNamespace']) ?
$this->attributes['proxyNamespace'] : null;
return $this->attributes['proxyNamespace'] ?? null;
}

/**
Expand Down Expand Up @@ -262,8 +257,7 @@ public function setHydratorDir($dir)
*/
public function getHydratorDir()
{
return isset($this->attributes['hydratorDir']) ?
$this->attributes['hydratorDir'] : null;
return $this->attributes['hydratorDir'] ?? null;
}

/**
Expand All @@ -274,8 +268,7 @@ public function getHydratorDir()
*/
public function getAutoGenerateHydratorClasses()
{
return isset($this->attributes['autoGenerateHydratorClasses']) ?
$this->attributes['autoGenerateHydratorClasses'] : true;
return $this->attributes['autoGenerateHydratorClasses'] ?? true;
}

/**
Expand All @@ -296,8 +289,7 @@ public function setAutoGenerateHydratorClasses($bool)
*/
public function getHydratorNamespace()
{
return isset($this->attributes['hydratorNamespace']) ?
$this->attributes['hydratorNamespace'] : null;
return $this->attributes['hydratorNamespace'] ?? null;
}

/**
Expand Down Expand Up @@ -327,8 +319,7 @@ public function setPersistentCollectionDir($dir)
*/
public function getPersistentCollectionDir()
{
return isset($this->attributes['persistentCollectionDir']) ?
$this->attributes['persistentCollectionDir'] : null;
return $this->attributes['persistentCollectionDir'] ?? null;
}

/**
Expand All @@ -339,8 +330,7 @@ public function getPersistentCollectionDir()
*/
public function getAutoGeneratePersistentCollectionClasses()
{
return isset($this->attributes['autoGeneratePersistentCollectionClasses']) ?
$this->attributes['autoGeneratePersistentCollectionClasses'] : self::AUTOGENERATE_ALWAYS;
return $this->attributes['autoGeneratePersistentCollectionClasses'] ?? self::AUTOGENERATE_ALWAYS;
}

/**
Expand All @@ -361,8 +351,7 @@ public function setAutoGeneratePersistentCollectionClasses($mode)
*/
public function getPersistentCollectionNamespace()
{
return isset($this->attributes['persistentCollectionNamespace']) ?
$this->attributes['persistentCollectionNamespace'] : null;
return $this->attributes['persistentCollectionNamespace'] ?? null;
}

/**
Expand Down Expand Up @@ -393,8 +382,7 @@ public function setDefaultDB($defaultDB)
*/
public function getDefaultDB()
{
return isset($this->attributes['defaultDB']) ?
$this->attributes['defaultDB'] : null;
return $this->attributes['defaultDB'] ?? null;
}

/**
Expand Down Expand Up @@ -427,11 +415,7 @@ public function getClassMetadataFactoryName()
*/
public function getDefaultCommitOptions()
{
if (isset($this->attributes['defaultCommitOptions'])) {
return $this->attributes['defaultCommitOptions'];
}

return array('w' => 1);
return $this->attributes['defaultCommitOptions'] ?? array('w' => 1);
}

/**
Expand Down Expand Up @@ -514,9 +498,7 @@ public function setDefaultRepositoryClassName($className)
*/
public function getDefaultRepositoryClassName()
{
return isset($this->attributes['defaultRepositoryClassName'])
? $this->attributes['defaultRepositoryClassName']
: DocumentRepository::class;
return $this->attributes['defaultRepositoryClassName'] ?? DocumentRepository::class;
}

/**
Expand All @@ -536,9 +518,7 @@ public function setRepositoryFactory(RepositoryFactory $repositoryFactory)
*/
public function getRepositoryFactory()
{
return isset($this->attributes['repositoryFactory'])
? $this->attributes['repositoryFactory']
: new DefaultRepositoryFactory();
return $this->attributes['repositoryFactory'] ?? new DefaultRepositoryFactory();
}

/**
Expand Down
4 changes: 1 addition & 3 deletions lib/Doctrine/ODM/MongoDB/DocumentManager.php
Expand Up @@ -713,9 +713,7 @@ public function createReference($document, array $referenceMapping)
* discriminator field and no value, so default to the full class name.
*/
if (isset($class->discriminatorField)) {
$reference[$class->discriminatorField] = isset($class->discriminatorValue)
? $class->discriminatorValue
: $class->name;
$reference[$class->discriminatorField] = $class->discriminatorValue ?? $class->name;
}

/* Add a discriminator value if the referenced document is not mapped
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataInfo.php
Expand Up @@ -642,7 +642,7 @@ public function hasLifecycleCallbacks($event)
*/
public function getLifecycleCallbacks($event)
{
return isset($this->lifecycleCallbacks[$event]) ? $this->lifecycleCallbacks[$event] : array();
return $this->lifecycleCallbacks[$event] ?? array();
}

/**
Expand Down
14 changes: 7 additions & 7 deletions lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php
Expand Up @@ -85,7 +85,7 @@ public function loadMetadataForClass($className, ClassMetadata $class)
* configurations, but fall back to "fieldName" for BC.
*/
$class->setDiscriminatorField(
isset($discrField['name']) ? (string) $discrField['name'] : (string) $discrField['fieldName']
(string) ($discrField['name'] ?? $discrField['fieldName'])
);
}
if (isset($xmlRoot->{'discriminator-map'})) {
Expand Down Expand Up @@ -234,7 +234,7 @@ private function addEmbedMapping(ClassMetadataInfo $class, $embed, $type)
'targetDocument' => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
'collectionClass' => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null,
'name' => (string) $attributes['field'],
'strategy' => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy,
'strategy' => (string) ($attributes['strategy'] ?? $defaultStrategy),
);
if (isset($attributes['fieldName'])) {
$mapping['fieldName'] = (string) $attributes['fieldName'];
Expand Down Expand Up @@ -274,11 +274,11 @@ private function addReferenceMapping(ClassMetadataInfo $class, $reference, $type
'orphanRemoval' => isset($attributes['orphan-removal']) ? ('true' === (string) $attributes['orphan-removal']) : false,
'type' => $type,
'reference' => true,
'storeAs' => isset($attributes['store-as']) ? (string) $attributes['store-as'] : ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF,
'storeAs' => (string) ($attributes['store-as'] ?? ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF),
'targetDocument' => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
'collectionClass' => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null,
'name' => (string) $attributes['field'],
'strategy' => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy,
'strategy' => (string) ($attributes['strategy'] ?? $defaultStrategy),
'inversedBy' => isset($attributes['inversed-by']) ? (string) $attributes['inversed-by'] : null,
'mappedBy' => isset($attributes['mapped-by']) ? (string) $attributes['mapped-by'] : null,
'repositoryMethod' => isset($attributes['repository-method']) ? (string) $attributes['repository-method'] : null,
Expand Down Expand Up @@ -306,7 +306,7 @@ private function addReferenceMapping(ClassMetadataInfo $class, $reference, $type
if (isset($reference->{'sort'})) {
foreach ($reference->{'sort'}->{'sort'} as $sort) {
$attr = $sort->attributes();
$mapping['sort'][(string) $attr['field']] = isset($attr['order']) ? (string) $attr['order'] : 'asc';
$mapping['sort'][(string) $attr['field']] = (string) ($attr['order'] ?? 'asc');
}
}
if (isset($reference->{'criteria'})) {
Expand Down Expand Up @@ -338,7 +338,7 @@ private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex)
$keys = array();

foreach ($xmlIndex->{'key'} as $key) {
$keys[(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc';
$keys[(string) $key['name']] = (string) ($key['order'] ?? 'asc');
}

$options = array();
Expand Down Expand Up @@ -443,7 +443,7 @@ private function setShardKey(ClassMetadataInfo $class, \SimpleXmlElement $xmlSha
$keys = array();
$options = array();
foreach ($xmlShardkey->{'key'} as $key) {
$keys[(string) $key['name']] = isset($key['order']) ? (string)$key['order'] : 'asc';
$keys[(string) $key['name']] = (string) ($key['order'] ?? 'asc');
}

if (isset($attributes['unique'])) {
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Mapping/Driver/YamlDriver.php
Expand Up @@ -241,7 +241,7 @@ private function addMappingFromEmbed(ClassMetadataInfo $class, $fieldName, $embe
'targetDocument' => $embed['targetDocument'] ?? null,
'collectionClass' => $embed['collectionClass'] ?? null,
'fieldName' => $fieldName,
'strategy' => isset($embed['strategy']) ? (string) $embed['strategy'] : $defaultStrategy,
'strategy' => (string) ($embed['strategy'] ?? $defaultStrategy),
);
if (isset($embed['name'])) {
$mapping['name'] = $embed['name'];
Expand All @@ -266,11 +266,11 @@ private function addMappingFromReference(ClassMetadataInfo $class, $fieldName, $
'orphanRemoval' => $reference['orphanRemoval'] ?? false,
'type' => $type,
'reference' => true,
'storeAs' => isset($reference['storeAs']) ? (string) $reference['storeAs'] : ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF,
'storeAs' => (string) ($reference['storeAs'] ?? ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF),
'targetDocument' => $reference['targetDocument'] ?? null,
'collectionClass' => $reference['collectionClass'] ?? null,
'fieldName' => $fieldName,
'strategy' => isset($reference['strategy']) ? (string) $reference['strategy'] : $defaultStrategy,
'strategy' => (string) ($reference['strategy'] ?? $defaultStrategy),
'inversedBy' => isset($reference['inversedBy']) ? (string) $reference['inversedBy'] : null,
'mappedBy' => isset($reference['mappedBy']) ? (string) $reference['mappedBy'] : null,
'repositoryMethod' => isset($reference['repositoryMethod']) ? (string) $reference['repositoryMethod'] : null,
Expand Down
6 changes: 2 additions & 4 deletions lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Expand Up @@ -689,9 +689,7 @@ private function loadEmbedManyCollection(PersistentCollectionInterface $collecti
$this->uow->setParentAssociation($embeddedDocumentObject, $mapping, $owner, $mapping['name'] . '.' . $key);

$data = $this->hydratorFactory->hydrate($embeddedDocumentObject, $embeddedDocument, $collection->getHints());
$id = $embeddedMetadata->identifier && isset($data[$embeddedMetadata->identifier])
? $data[$embeddedMetadata->identifier]
: null;
$id = $embeddedMetadata->identifier && $data[$embeddedMetadata->identifier] ?? null;

if (empty($collection->getHints()[Query::HINT_READ_ONLY])) {
$this->uow->registerManaged($embeddedDocumentObject, $id, $data);
Expand Down Expand Up @@ -796,7 +794,7 @@ public function createReferenceManyInverseSideQuery(PersistentCollectionInterfac
$owner = $collection->getOwner();
$ownerClass = $this->dm->getClassMetadata(get_class($owner));
$targetClass = $this->dm->getClassMetadata($mapping['targetDocument']);
$mappedByMapping = isset($targetClass->fieldMappings[$mapping['mappedBy']]) ? $targetClass->fieldMappings[$mapping['mappedBy']] : array();
$mappedByMapping = $targetClass->fieldMappings[$mapping['mappedBy']] ?? array();
$mappedByFieldName = ClassMetadataInfo::getReferenceFieldName($mappedByMapping['storeAs'] ?? ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF, $mapping['mappedBy']);

$criteria = $this->cm->merge(
Expand Down
12 changes: 3 additions & 9 deletions lib/Doctrine/ODM/MongoDB/Persisters/PersistenceBuilder.php
Expand Up @@ -94,9 +94,7 @@ public function prepareInsertData($document)

// add discriminator if the class has one
if (isset($class->discriminatorField)) {
$insertData[$class->discriminatorField] = isset($class->discriminatorValue)
? $class->discriminatorValue
: $class->name;
$insertData[$class->discriminatorField] = $class->discriminatorValue ?? $class->name;
}

return $insertData;
Expand Down Expand Up @@ -271,9 +269,7 @@ public function prepareUpsertData($document)

// add discriminator if the class has one
if (isset($class->discriminatorField)) {
$updateData['$set'][$class->discriminatorField] = isset($class->discriminatorValue)
? $class->discriminatorValue
: $class->name;
$updateData['$set'][$class->discriminatorField] = $class->discriminatorValue ?? $class->name;
}

return $updateData;
Expand Down Expand Up @@ -394,9 +390,7 @@ public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDo
* discriminator field and no value, so default to the full class name.
*/
if (isset($class->discriminatorField)) {
$embeddedDocumentValue[$class->discriminatorField] = isset($class->discriminatorValue)
? $class->discriminatorValue
: $class->name;
$embeddedDocumentValue[$class->discriminatorField] = $class->discriminatorValue ?? $class->name;
}

// Ensure empty embedded documents are stored as BSON objects
Expand Down
6 changes: 1 addition & 5 deletions lib/Doctrine/ODM/MongoDB/SchemaManager.php
Expand Up @@ -523,11 +523,7 @@ public function ensureDocumentSharding($documentName, array $indexOptions = arra
if ($result['ok'] != 1 && strpos($result['errmsg'], 'please create an index that starts') !== false) {
// The proposed key is not returned when using mongo-php-adapter with ext-mongodb.
// See https://github.com/mongodb/mongo-php-driver/issues/296 for details
if (isset($result['proposedKey'])) {
$key = $result['proposedKey'];
} else {
$key = $this->dm->getClassMetadata($documentName)->getShardKey()['keys'];
}
$key = $result['proposedKey'] ?? $this->dm->getClassMetadata($documentName)->getShardKey()['keys'];

$this->dm->getDocumentCollection($documentName)->ensureIndex($key, $indexOptions);
$done = false;
Expand Down

0 comments on commit 66dea15

Please sign in to comment.