Applied symplify rules to codebase.

This commit is contained in:
Jan Böhmer 2020-01-05 22:49:00 +01:00
parent 2f20d90041
commit 388e847b17
136 changed files with 1370 additions and 789 deletions

View file

@ -44,6 +44,7 @@ declare(strict_types=1);
namespace App\Entity\Base;
use Doctrine\ORM\Mapping as ORM;
use function is_string;
use Symfony\Component\Validator\Constraints as Assert;
/**
@ -157,7 +158,7 @@ abstract class Company extends PartsContainingDBElement
*/
public function getAutoProductUrl($partnr = null): string
{
if (\is_string($partnr)) {
if (is_string($partnr)) {
return str_replace('%PARTNUMBER%', $partnr, $this->auto_product_url);
}

View file

@ -25,9 +25,13 @@ namespace App\Entity\Base;
use App\Entity\Attachments\AttachmentContainingDBElement;
use App\Validator\Constraints\NoneOfItsChildren;
use function count;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use function get_class;
use InvalidArgumentException;
use function is_array;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
@ -50,23 +54,11 @@ abstract class StructuralDBElement extends AttachmentContainingDBElement
{
public const ID_ROOT_ELEMENT = 0;
/** This is a not standard character, so build a const, so a dev can easily use it */
/**
* This is a not standard character, so build a const, so a dev can easily use it
*/
public const PATH_DELIMITER_ARROW = ' → ';
/**
* We can not define the mapping here or we will get an exception. Unfortunately we have to do the mapping in the
* subclasses
* @var StructuralDBElement[]
* @Groups({"include_children"})
*/
protected $children;
/**
* @var StructuralDBElement
* @NoneOfItsChildren()
* @Groups({"include_parents"})
*/
protected $parent;
/**
* @var string The comment info for this element
* @ORM\Column(type="text")
@ -86,10 +78,25 @@ abstract class StructuralDBElement extends AttachmentContainingDBElement
*/
protected $level = 0;
/**
* We can not define the mapping here or we will get an exception. Unfortunately we have to do the mapping in the
* subclasses.
*
* @var StructuralDBElement[]
* @Groups({"include_children"})
*/
protected $children = [];
/**
* @var StructuralDBElement
* @NoneOfItsChildren()
* @Groups({"include_parents"})
*/
protected $parent;
/** @var string[] all names of all parent elements as a array of strings,
* the last array element is the name of the element itself
*/
private $full_path_strings;
private $full_path_strings = [];
public function __construct()
{
@ -109,7 +116,7 @@ abstract class StructuralDBElement extends AttachmentContainingDBElement
*
* @return bool True, if this element is child of $another_element.
*
* @throws \InvalidArgumentException if there was an error
* @throws InvalidArgumentException if there was an error
*/
public function isChildOf(self $another_element): bool
{
@ -117,8 +124,8 @@ abstract class StructuralDBElement extends AttachmentContainingDBElement
//Check if both elements compared, are from the same type
// (we have to check inheritance, or we get exceptions when using doctrine entities (they have a proxy type):
if (! is_a($another_element, $class_name) && ! is_a($this, \get_class($another_element))) {
throw new \InvalidArgumentException('isChildOf() only works for objects of the same type!');
if (! is_a($another_element, $class_name) && ! is_a($this, get_class($another_element))) {
throw new InvalidArgumentException('isChildOf() only works for objects of the same type!');
}
if (null === $this->getParent()) { // this is the root node
@ -201,7 +208,7 @@ abstract class StructuralDBElement extends AttachmentContainingDBElement
*/
public function getFullPath(string $delimiter = self::PATH_DELIMITER_ARROW): string
{
if (! \is_array($this->full_path_strings)) {
if (empty($this->full_path_strings)) {
$this->full_path_strings = [];
$this->full_path_strings[] = $this->getName();
$element = $this;
@ -233,7 +240,7 @@ abstract class StructuralDBElement extends AttachmentContainingDBElement
$tmp[] = $this;
//We only allow 20 levels depth
while (! end($tmp)->isRoot() && \count($tmp) < 20) {
while (! end($tmp)->isRoot() && count($tmp) < 20) {
$tmp[] = end($tmp)->parent;
}

View file

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace App\Entity\Base;
use DateTime;
use Symfony\Component\Serializer\Annotation\Groups;
/**
@ -32,14 +33,14 @@ use Symfony\Component\Serializer\Annotation\Groups;
trait TimestampTrait
{
/**
* @var \DateTime the date when this element was modified the last time
* @var DateTime the date when this element was modified the last time
* @ORM\Column(type="datetime", name="last_modified", options={"default"="CURRENT_TIMESTAMP"})
* @Groups({"extended", "full"})
*/
protected $lastModified;
/**
* @var \DateTime the date when this element was created
* @var DateTime the date when this element was created
* @ORM\Column(type="datetime", name="datetime_added", options={"default"="CURRENT_TIMESTAMP"})
* @Groups({"extended", "full"})
*/
@ -49,9 +50,9 @@ trait TimestampTrait
* Returns the last time when the element was modified.
* Returns null if the element was not yet saved to DB yet.
*
* @return \DateTime|null the time of the last edit
* @return DateTime|null the time of the last edit
*/
public function getLastModified(): ?\DateTime
public function getLastModified(): ?DateTime
{
return $this->lastModified;
}
@ -60,9 +61,9 @@ trait TimestampTrait
* Returns the date/time when the element was created.
* Returns null if the element was not yet saved to DB yet.
*
* @return \DateTime|null the creation time of the part
* @return DateTime|null the creation time of the part
*/
public function getAddedDate(): ?\DateTime
public function getAddedDate(): ?DateTime
{
return $this->addedDate;
}
@ -75,9 +76,9 @@ trait TimestampTrait
*/
public function updatedTimestamps(): void
{
$this->lastModified = new \DateTime('now');
$this->lastModified = new DateTime('now');
if (null === $this->addedDate) {
$this->addedDate = new \DateTime('now');
$this->addedDate = new DateTime('now');
}
}
}