Added an PHP CS fixer config file and applied it to files.

We now use the same the same style as the symfony project, and it allows us to simply fix the style by executing php_cs_fixer fix in the project root.
This commit is contained in:
Jan Böhmer 2019-11-09 00:47:20 +01:00
parent 89258bc102
commit e557bdedd5
210 changed files with 2099 additions and 2742 deletions

View file

@ -1,6 +1,6 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony)
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
*
@ -17,16 +17,12 @@
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Services;
use App\Entity\Base\NamedDBElement;
use App\Entity\Base\StructuralDBElement;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Serializer\SerializerInterface;
@ -34,7 +30,6 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
class EntityImporter
{
protected $serializer;
protected $em;
protected $validator;
@ -53,25 +48,26 @@ class EntityImporter
'format' => 'json',
'preserve_children' => true,
'parent' => null,
'abort_on_validation_error' => true
'abort_on_validation_error' => true,
]);
}
/**
* Creates many entries at once, based on a (text) list of names.
*
* @param string $lines The list of names seperated by \n
* @param string $class_name The name of the class for which the entities should be created
* @param StructuralDBElement|null $parent The element which will be used as parent element for new elements.
* @param string $lines The list of names seperated by \n
* @param string $class_name The name of the class for which the entities should be created
* @param StructuralDBElement|null $parent The element which will be used as parent element for new elements.
*
* @return array An associative array containing an ConstraintViolationList and the entity name as key are returned,
* if an error happened during validation. When everything was successfull, the array should be empty.
* if an error happened during validation. When everything was successfull, the array should be empty.
*/
public function massCreation(string $lines, string $class_name, ?StructuralDBElement $parent) : array
public function massCreation(string $lines, string $class_name, ?StructuralDBElement $parent): array
{
//Expand every line to a single entry:
$names = explode("\n", $lines);
$errors = array();
$errors = [];
foreach ($names as $name) {
$name = trim($name);
@ -84,7 +80,7 @@ class EntityImporter
//Validate entity
$tmp = $this->validator->validate($entity);
//If no error occured, write entry to DB:
if (count($tmp) === 0) {
if (0 === \count($tmp)) {
$this->em->persist($entity);
} else { //Otherwise log error
dump($tmp);
@ -101,27 +97,28 @@ class EntityImporter
/**
* This methods deserializes the given file and saves it database.
* The imported elements will be checked (validated) before written to database.
* @param File $file The file that should be used for importing.
*
* @param File $file The file that should be used for importing.
* @param string $class_name The class name of the enitity that should be imported.
* @param array $options Options for the import process.
* @param array $options Options for the import process.
*
* @return array An associative array containing an ConstraintViolationList and the entity name as key are returned,
* if an error happened during validation. When everything was successfull, the array should be empty.
* if an error happened during validation. When everything was successfull, the array should be empty.
*/
public function fileToDBEntities(File $file, string $class_name, array $options = []) : array
public function fileToDBEntities(File $file, string $class_name, array $options = []): array
{
$resolver = new OptionsResolver();
$this->configureOptions($resolver);
$options = $resolver->resolve($options);
$entities = $this->fileToEntityArray($file, $class_name, $options);
$errors = array();
$errors = [];
//Iterate over each $entity write it to DB.
foreach ($entities as $entity) {
/** @var StructuralDBElement $entity */
/* @var StructuralDBElement $entity */
//Move every imported entity to the selected parent
$entity->setParent($options['parent']);
@ -129,7 +126,7 @@ class EntityImporter
$tmp = $this->validator->validate($entity);
//When no validation error occured, persist entity to database (cascade must be set in entity)
if (count($errors) === 0) {
if (0 === \count($errors)) {
$this->em->persist($entity);
} else { //Log validation errors to global log.
$errors[$entity->getFullPath()] = $tmp;
@ -137,7 +134,7 @@ class EntityImporter
}
//Save changes to database, when no error happened, or we should continue on error.
if (empty($errors) || $options['abort_on_validation_error'] == false) {
if (empty($errors) || false == $options['abort_on_validation_error']) {
$this->em->flush();
}
@ -148,12 +145,14 @@ class EntityImporter
* This method converts (deserialize) a (uploaded) file to an array of entities with the given class.
*
* The imported elements will NOT be validated. If you want to use the result array, you have to validate it by yourself.
* @param File $file The file that should be used for importing.
*
* @param File $file The file that should be used for importing.
* @param string $class_name The class name of the enitity that should be imported.
* @param array $options Options for the import process.
* @param array $options Options for the import process.
*
* @return array An array containing the deserialized elements.
*/
public function fileToEntityArray(File $file, string $class_name, array $options = []) : array
public function fileToEntityArray(File $file, string $class_name, array $options = []): array
{
$resolver = new OptionsResolver();
$this->configureOptions($resolver);
@ -170,11 +169,11 @@ class EntityImporter
}
//The [] behind class_name denotes that we expect an array.
$entities = $this->serializer->deserialize($content, $class_name . '[]', $options['format'],
$entities = $this->serializer->deserialize($content, $class_name.'[]', $options['format'],
['groups' => $groups, 'csv_delimiter' => $options['csv_separator']]);
//Ensure we have an array of entitity elements.
if(!is_array($entities)) {
if (!\is_array($entities)) {
$entities = [$entities];
}
@ -188,16 +187,17 @@ class EntityImporter
/**
* This functions corrects the parent setting based on the children value of the parent.
*
* @param iterable $entities The list of entities that should be fixed.
* @param null $parent The parent, to which the entity should be set.
* @param null $parent The parent, to which the entity should be set.
*/
protected function correctParentEntites(iterable $entities, $parent = null)
{
foreach ($entities as $entity) {
/** @var $entity StructuralDBElement */
/* @var $entity StructuralDBElement */
$entity->setParent($parent);
//Do the same for the children of entity
$this->correctParentEntites($entity->getChildren(), $entity);
}
}
}
}