Please note that this service is under alpha statement, we are actively working on it. It may be unstable and might completely change without warning. Help us by reporting!

sigmapix/SigmapixSonataImportBundle

Analysis #10763 FAILED

Diff

Download raw diff

diff --git a/Admin/ImportableAdminTrait.php b/Admin/ImportableAdminTrait.php
index 9e6df55f59a251b105328d906182cd6fa317d8e1..0d116290b47ecf25295fc9eefd6d8f4e5aef5623 100644
--- a/Admin/ImportableAdminTrait.php
+++ b/Admin/ImportableAdminTrait.php
@@ -24,8 +24,7 @@ use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Translation\TranslatorInterface;
 
 /**
- * Trait ImportableAdminTrait
- * @package Sigmapix\Sonata\ImportBundle\Admin
+ * Trait ImportableAdminTrait.
  */
 trait ImportableAdminTrait
 {
@@ -82,8 +81,8 @@ trait ImportableAdminTrait
 
     /**
      * @param FormBuilderInterface $formBuilder
-     * @param array $headers
-     * todo: use defineFormBuilder for import Action and upload Action
+     * @param array                $headers
+     *                                          todo: use defineFormBuilder for import Action and upload Action
      */
     public function defineImportFormBuilder(FormBuilderInterface $formBuilder, array $headers)
     {
@@ -108,14 +107,14 @@ trait ImportableAdminTrait
                     'choices' => $headers,
                     'data' => $this->nearest($field->getOption('label'), $headers, $trans),
                     'mapped' => false,
-                    'label' => $field->getOption('label')
+                    'label' => $field->getOption('label'),
                 ]);
-            } elseif ((string) $propertyPath === 'id') {
+            } elseif ('id' === (string) $propertyPath) {
                 $mapper->add($field->getName(), ImportFieldChoiceType::class, [
                     'choices' => $headers,
                     'data' => $this->nearest($field->getOption('label'), $headers, $trans),
                     'mapped' => false,
-                    'label' => $field->getOption('label')
+                    'label' => $field->getOption('label'),
                 ]);
             } else {
                 $mapper->add($field->getName(), ImportFieldChoiceType::class, [
@@ -124,7 +123,7 @@ trait ImportableAdminTrait
                     'mapped' => $field->getOption('mapped'),
                     'label' => $field->getOption('label'),
                     'label_format' => $field->getOption('label_format'), // This will be used for DateTimeConverter
-                    'translation_domain' => $field->getOption('translation_domain')
+                    'translation_domain' => $field->getOption('translation_domain'),
                 ]);
             }
         }
@@ -136,25 +135,30 @@ trait ImportableAdminTrait
     /**
      * @param $admin
      * @param null $object
+     *
      * @return mixed
      */
     public function configureActionButtons($admin, $object = null)
     {
         $buttonList = parent::configureActionButtons($admin, $object);
         $buttonList['import'] = [
-            'template' => 'SigmapixSonataImportBundle:Button:import_button.html.twig'
+            'template' => 'SigmapixSonataImportBundle:Button:import_button.html.twig',
         ];
+
         return $buttonList;
     }
 
     /**
      * @param array $headers
-     * @return Form
+     *
      * @throws \ReflectionException
+     *
+     * @return Form
      */
     public function getImportForm(array $headers)
     {
         $this->buildImportForm($headers);
+
         return $this->importForm;
     }
 
@@ -232,6 +236,7 @@ trait ImportableAdminTrait
 
     /**
      * @param array $headers
+     *
      * @throws \ReflectionException
      */
     protected function buildImportForm(array $headers)
@@ -246,7 +251,8 @@ trait ImportableAdminTrait
      * @param $input
      * @param $words
      * @param TranslatorInterface $trans
-     * @param string $domain
+     * @param string              $domain
+     *
      * @return string
      */
     private function nearest($input, $words, TranslatorInterface $trans, $domain = null)
@@ -262,7 +268,7 @@ trait ImportableAdminTrait
             $levCase = levenshtein(strtolower($input), strtolower($wordASCII));
             $levTrans = levenshtein($trans->trans($input, [], $domain), $wordASCII);
             $lev = min([$lev, $levCase, $levTrans]);
-            if ($lev === 0) {
+            if (0 === $lev) {
                 $closest = $word;
                 break;
             }
diff --git a/Controller/CRUDController.php b/Controller/CRUDController.php
index ff5a6cb83c2e9e6540fae4570e23729cf75de3d7..af9ab7179ea1f70ef789cced777733480dbdcd13 100644
--- a/Controller/CRUDController.php
+++ b/Controller/CRUDController.php
@@ -56,7 +56,7 @@ class CRUDController extends Controller
             $exporter = $this->get('sonata.exporter.exporter');
         }
 
-        if (!in_array($format, $allowedExportFormats)) {
+        if (!\in_array($format, $allowedExportFormats)) {
             throw new \RuntimeException(
                 sprintf(
                     'Export in format `%s` is not allowed for class: `%s`. Allowed formats are: `%s`',
diff --git a/Service/ImportService.php b/Service/ImportService.php
index f11d1ed6c859bd66849e4d487f378a1c27107cbd..fd9e8bb4245d7eef7804f39329d6755e9ee6963f 100644
--- a/Service/ImportService.php
+++ b/Service/ImportService.php
@@ -62,20 +62,8 @@ final class ImportService
         $reader = $this->getReader($file);
         $headers = array_flip($this->fixHeadersEncoding($reader));
         array_walk($headers, function (&$v, $k) use ($headers) { $v = $k; });
-        return $headers;
-    }
 
-    private function fixHeadersEncoding($reader)
-    {
-        $columnHeaders = array_filter($reader->getColumnHeaders(), function ($h) {return null !== $h; });
-        $columnHeaders = array_map(
-            function ($h) {
-                if (!empty($h) && is_string($h) && mb_detect_encoding($h) !== 'UTF-8') {
-                    $h = utf8_encode($h);
-                }
-                return trim($h);
-            }, $columnHeaders);
-        return $columnHeaders;
+        return $headers;
     }
 
     /**
@@ -99,12 +87,13 @@ final class ImportService
         // Replace columnsHeader names with entity field name in our $mapping
         $columnHeaders = array_map(function ($h) use ($mapping) {
             $k = array_search($h, (array) $mapping, true);
+
             return false === $k ? $h : $k;
         }, $this->fixHeadersEncoding($reader));
         $reader->setColumnHeaders($columnHeaders);
 
         /** @var DoctrineWriter $writer */
-        $writer = new $this->doctrineWriterClass($this->em, get_class($form->getData()));
+        $writer = new $this->doctrineWriterClass($this->em, \get_class($form->getData()));
         if (method_exists($writer, 'setContainer')) {
             $writer->setContainer($this->container);
         }
@@ -141,6 +130,21 @@ final class ImportService
         return $this;
     }
 
+    private function fixHeadersEncoding($reader)
+    {
+        $columnHeaders = array_filter($reader->getColumnHeaders(), function ($h) {return null !== $h; });
+        $columnHeaders = array_map(
+            function ($h) {
+                if (!empty($h) && \is_string($h) && 'UTF-8' !== mb_detect_encoding($h)) {
+                    $h = utf8_encode($h);
+                }
+
+                return trim($h);
+            }, $columnHeaders);
+
+        return $columnHeaders;
+    }
+
     /**
      * @param UploadedFile $file
      *
@@ -152,7 +156,7 @@ final class ImportService
         $fileExtension = $file->guessExtension();
         $excelExtensions = ['xls', 'xlsx', 'zip'];
 
-        if (in_array($fileExtension, $excelExtensions)) {
+        if (\in_array($fileExtension, $excelExtensions)) {
             $reader = new ExcelReader(new \SplFileObject($pathFile), 0, 0, true);
         } else {
             $reader = new CsvReader(new \SplFileObject($pathFile), ';');