Skip to content
Permalink
Browse files

Merge pull request #63 from ergebnis/fix/rename

Fix: Rename methods, parameters, variables, and fields
  • Loading branch information
localheinz committed Mar 16, 2020
2 parents b04ef66 + 7df9707 commit 2850a1a5c10166b7b1f7cdcd343a02b53cbd04c0
Showing with 74 additions and 74 deletions.
  1. +6 −6 phpstan-baseline.neon
  2. +19 −19 psalm-baseline.xml
  3. +49 −49 src/FixtureFactory.php
@@ -51,7 +51,7 @@ parameters:
path: src/FixtureFactory.php

-
message: "#^Variable \\$metadata in isset\\(\\) always exists and is not nullable\\.$#"
message: "#^Variable \\$classMetadata in isset\\(\\) always exists and is not nullable\\.$#"
count: 1
path: src/FixtureFactory.php

@@ -81,7 +81,7 @@ parameters:
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:setField\\(\\) has parameter \\$ent with no typehint specified\\.$#"
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:setField\\(\\) has parameter \\$entity with no typehint specified\\.$#"
count: 1
path: src/FixtureFactory.php

@@ -106,22 +106,22 @@ parameters:
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:updateCollectionSideOfAssocation\\(\\) has parameter \\$entityBeingCreated with no typehint specified\\.$#"
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:updateCollectionSideOfAssocation\\(\\) has parameter \\$classMetadata with no typehint specified\\.$#"
count: 1
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:updateCollectionSideOfAssocation\\(\\) has parameter \\$fieldName with no typehint specified\\.$#"
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:updateCollectionSideOfAssocation\\(\\) has parameter \\$entity with no typehint specified\\.$#"
count: 1
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:updateCollectionSideOfAssocation\\(\\) has parameter \\$metadata with no typehint specified\\.$#"
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:updateCollectionSideOfAssocation\\(\\) has parameter \\$fieldName with no typehint specified\\.$#"
count: 1
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:updateCollectionSideOfAssocation\\(\\) has parameter \\$value with no typehint specified\\.$#"
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:updateCollectionSideOfAssocation\\(\\) has parameter \\$fieldValue with no typehint specified\\.$#"
count: 1
path: src/FixtureFactory.php

@@ -59,17 +59,17 @@
</file>
<file src="src/FixtureFactory.php">
<DocblockTypeContradiction occurrences="1">
<code>isset($metadata)</code>
<code>isset($classMetadata)</code>
</DocblockTypeContradiction>
<MissingParamType occurrences="8">
<code>$ent</code>
<code>$entity</code>
<code>$fieldName</code>
<code>$fieldValue</code>
<code>$array</code>
<code>$entityBeingCreated</code>
<code>$metadata</code>
<code>$entity</code>
<code>$classMetadata</code>
<code>$fieldName</code>
<code>$value</code>
<code>$fieldValue</code>
</MissingParamType>
<MissingReturnType occurrences="4">
<code>get</code>
@@ -86,23 +86,23 @@
<code>$fieldName</code>
<code>$name</code>
<code>$name</code>
<code>$def-&gt;getFieldDefinitions()</code>
<code>$entityDefinition-&gt;getFieldDefinitions()</code>
<code>$fieldName</code>
<code>$ent</code>
<code>$entity</code>
<code>$fieldName</code>
<code>$ent</code>
<code>$entity</code>
<code>$fieldName</code>
<code>$fieldName</code>
<code>$value</code>
<code>$value</code>
<code>$inverse</code>
<code>$fieldValue</code>
<code>$fieldValue</code>
<code>$inversedBy</code>
</MixedArgument>
<MixedArrayAccess occurrences="1">
<code>$assoc['inversedBy']</code>
<code>$association['inversedBy']</code>
</MixedArrayAccess>
<MixedArrayOffset occurrences="11">
<code>$this-&gt;entityDefs[$name]</code>
<code>$this-&gt;entityDefs[$name]</code>
<code>$this-&gt;entityDefinitions[$name]</code>
<code>$this-&gt;entityDefinitions[$name]</code>
<code>$this-&gt;singletons[$name]</code>
<code>$this-&gt;singletons[$name]</code>
<code>$fieldOverrides[$fieldName]</code>
@@ -115,18 +115,18 @@
</MixedArrayOffset>
<MixedAssignment occurrences="9">
<code>$type</code>
<code>$fieldDef</code>
<code>$fieldDefinition</code>
<code>$fieldValues[$fieldName]</code>
<code>$fieldValue</code>
<code>$instances[]</code>
<code>$this-&gt;persist</code>
<code>$assoc</code>
<code>$inverse</code>
<code>$association</code>
<code>$inversedBy</code>
<code>$collection</code>
</MixedAssignment>
<MixedFunctionCall occurrences="2">
<code>$fieldDef($this)</code>
<code>$config['afterCreate']($ent, $fieldValues)</code>
<code>$fieldDefinition($this)</code>
<code>$configuration['afterCreate']($entity, $fieldValues)</code>
</MixedFunctionCall>
<MixedMethodCall occurrences="1">
<code>getAssociationMapping</code>
@@ -26,12 +26,12 @@ final class FixtureFactory
/**
* @var ORM\EntityManagerInterface
*/
private $em;
private $entityManager;

/**
* @var array<EntityDef>
*/
private $entityDefs;
private $entityDefinitions;

/**
* @var array
@@ -43,10 +43,10 @@ final class FixtureFactory
*/
private $persist;

public function __construct(ORM\EntityManagerInterface $em)
public function __construct(ORM\EntityManagerInterface $entityManager)
{
$this->em = $em;
$this->entityDefs = [];
$this->entityManager = $entityManager;
$this->entityDefinitions = [];
$this->singletons = [];
$this->persist = false;
}
@@ -57,14 +57,14 @@ public function __construct(ORM\EntityManagerInterface $em)
* See the readme for a tutorial.
*
* @param mixed $name
* @param array $fieldDefs
* @param array $config
* @param array $fieldDefinitions
* @param array $configuration
*
* @return FixtureFactory
*/
public function defineEntity($name, array $fieldDefs = [], array $config = [])
public function defineEntity($name, array $fieldDefinitions = [], array $configuration = [])
{
if (isset($this->entityDefs[$name])) {
if (isset($this->entityDefinitions[$name])) {
throw new \Exception(\sprintf(
"Entity '%s' already defined in fixture factory",
$name
@@ -80,19 +80,19 @@ public function defineEntity($name, array $fieldDefs = [], array $config = [])
));
}

$metadata = $this->em->getClassMetadata($type);
$classMetadata = $this->entityManager->getClassMetadata($type);

if (!isset($metadata)) {
if (!isset($classMetadata)) {
throw new \Exception(\sprintf(
'Unknown entity type: %s',
$type
));
}

$this->entityDefs[$name] = new EntityDef(
$metadata,
$fieldDefs,
$config
$this->entityDefinitions[$name] = new EntityDef(
$classMetadata,
$fieldDefinitions,
$configuration
);

return $this;
@@ -117,43 +117,43 @@ public function get($name, array $fieldOverrides = [])
return $this->singletons[$name];
}

if (!\array_key_exists($name, $this->entityDefs)) {
if (!\array_key_exists($name, $this->entityDefinitions)) {
throw Exception\EntityDefinitionUnavailable::for($name);
}

/** @var EntityDef $def */
$def = $this->entityDefs[$name];
/** @var EntityDef $entityDefinition */
$entityDefinition = $this->entityDefinitions[$name];

$config = $def->getConfiguration();
$configuration = $entityDefinition->getConfiguration();

$this->checkFieldOverrides($def, $fieldOverrides);
$this->checkFieldOverrides($entityDefinition, $fieldOverrides);

/** @var ORM\Mapping\ClassMetadata $entityMetadata */
$entityMetadata = $def->getClassMetadata();
/** @var ORM\Mapping\ClassMetadata $classMetadata */
$classMetadata = $entityDefinition->getClassMetadata();

$ent = $entityMetadata->newInstance();
$entity = $classMetadata->newInstance();

$fieldValues = [];

foreach ($def->getFieldDefinitions() as $fieldName => $fieldDef) {
foreach ($entityDefinition->getFieldDefinitions() as $fieldName => $fieldDefinition) {
$fieldValues[$fieldName] = \array_key_exists($fieldName, $fieldOverrides)
? $fieldOverrides[$fieldName]
: $fieldDef($this);
: $fieldDefinition($this);
}

foreach ($fieldValues as $fieldName => $fieldValue) {
$this->setField($ent, $def, $fieldName, $fieldValue);
$this->setField($entity, $entityDefinition, $fieldName, $fieldValue);
}

if (isset($config['afterCreate'])) {
$config['afterCreate']($ent, $fieldValues);
if (isset($configuration['afterCreate'])) {
$configuration['afterCreate']($entity, $fieldValues);
}

if ($this->persist && false === $entityMetadata->isEmbeddedClass) {
$this->em->persist($ent);
if ($this->persist && false === $classMetadata->isEmbeddedClass) {
$this->entityManager->persist($entity);
}

return $ent;
return $entity;
}

/**
@@ -251,33 +251,33 @@ public function unsetSingleton($name): void
*/
public function definitions(): array
{
return $this->entityDefs;
return $this->entityDefinitions;
}

private function checkFieldOverrides(EntityDef $def, array $fieldOverrides): void
private function checkFieldOverrides(EntityDef $entityDefinition, array $fieldOverrides): void
{
$extraFields = \array_diff(\array_keys($fieldOverrides), \array_keys($def->getFieldDefinitions()));
$extraFields = \array_diff(\array_keys($fieldOverrides), \array_keys($entityDefinition->getFieldDefinitions()));

if (!empty($extraFields)) {
throw new \Exception(\sprintf(
'Field(s) not in %s: \'%s\'',
$def->getClassName(),
$entityDefinition->getClassName(),
\implode("', '", $extraFields)
));
}
}

private function setField($ent, EntityDef $def, $fieldName, $fieldValue): void
private function setField($entity, EntityDef $entityDefinition, $fieldName, $fieldValue): void
{
$metadata = $def->getClassMetadata();
$classMetadata = $entityDefinition->getClassMetadata();

if ($metadata->isCollectionValuedAssociation($fieldName)) {
$metadata->setFieldValue($ent, $fieldName, $this->createCollectionFrom($fieldValue));
if ($classMetadata->isCollectionValuedAssociation($fieldName)) {
$classMetadata->setFieldValue($entity, $fieldName, $this->createCollectionFrom($fieldValue));
} else {
$metadata->setFieldValue($ent, $fieldName, $fieldValue);
$classMetadata->setFieldValue($entity, $fieldName, $fieldValue);

if (\is_object($fieldValue) && $metadata->isSingleValuedAssociation($fieldName)) {
$this->updateCollectionSideOfAssocation($ent, $metadata, $fieldName, $fieldValue);
if (\is_object($fieldValue) && $classMetadata->isSingleValuedAssociation($fieldName)) {
$this->updateCollectionSideOfAssocation($entity, $classMetadata, $fieldName, $fieldValue);
}
}
}
@@ -291,18 +291,18 @@ private function createCollectionFrom($array = [])
return new Common\Collections\ArrayCollection();
}

private function updateCollectionSideOfAssocation($entityBeingCreated, $metadata, $fieldName, $value): void
private function updateCollectionSideOfAssocation($entity, $classMetadata, $fieldName, $fieldValue): void
{
$assoc = $metadata->getAssociationMapping($fieldName);
$association = $classMetadata->getAssociationMapping($fieldName);

$inverse = $assoc['inversedBy'];
$inversedBy = $association['inversedBy'];

if ($inverse) {
$valueMetadata = $this->em->getClassMetadata(\get_class($value));
$collection = $valueMetadata->getFieldValue($value, $inverse);
if ($inversedBy) {
$classMetadataOfFieldValue = $this->entityManager->getClassMetadata(\get_class($fieldValue));
$collection = $classMetadataOfFieldValue->getFieldValue($fieldValue, $inversedBy);

if ($collection instanceof Common\Collections\Collection) {
$collection->add($entityBeingCreated);
$collection->add($entity);
}
}
}

0 comments on commit 2850a1a

Please sign in to comment.
You can’t perform that action at this time.