Prechádzať zdrojové kódy

Fix validator deprecated methods usage

Sullivan SENECHAL 9 rokov pred
rodič
commit
baa54ad3cf
1 zmenil súbory, kde vykonal 50 pridanie a 19 odobranie
  1. 50 19
      src/Packagist/WebBundle/Entity/Package.php

+ 50 - 19
src/Packagist/WebBundle/Entity/Package.php

@@ -18,7 +18,7 @@ use Composer\Repository\VcsRepository;
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Validator\Constraints as Assert;
-use Symfony\Component\Validator\ExecutionContextInterface;
+use Symfony\Component\Validator\Context\ExecutionContextInterface;
 
 /**
  * @ORM\Entity(repositoryClass="Packagist\WebBundle\Entity\PackageRepository")
@@ -218,11 +218,20 @@ class Package
         $driver = $this->vcsDriver;
         if (!is_object($driver)) {
             if (preg_match('{https?://.+@}', $this->repository)) {
-                $context->addViolationAt($property, 'URLs with user@host are not supported, use a read-only public URL', array(), null);
+                $context->buildViolation('URLs with user@host are not supported, use a read-only public URL')
+                    ->atPath($property)
+                    ->addViolation()
+                ;
             } elseif (is_string($this->vcsDriverError)) {
-                $context->addViolationAt($property, 'Uncaught Exception: '.$this->vcsDriverError, array(), null);
+                $context->buildViolation('Uncaught Exception: '.$this->vcsDriverError)
+                    ->atPath($property)
+                    ->addViolation()
+                ;
             } else {
-                $context->addViolationAt($property, 'No valid/supported repository was found at the given URL', array(), null);
+                $context->buildViolation('No valid/supported repository was found at the given URL')
+                    ->atPath($property)
+                    ->addViolation()
+                ;
             }
             return;
         }
@@ -230,22 +239,34 @@ class Package
             $information = $driver->getComposerInformation($driver->getRootIdentifier());
 
             if (false === $information) {
-                $context->addViolationAt($property, 'No composer.json was found in the '.$driver->getRootIdentifier().' branch.', array(), null);
+                $context->buildViolation('No composer.json was found in the '.$driver->getRootIdentifier().' branch.')
+                    ->atPath($property)
+                    ->addViolation()
+                ;
                 return;
             }
 
             if (empty($information['name'])) {
-                $context->addViolationAt($property, 'The package name was not found in the composer.json, make sure there is a name present.', array(), null);
+                $context->buildViolation('The package name was not found in the composer.json, make sure there is a name present.')
+                    ->atPath($property)
+                    ->addViolation()
+                ;
                 return;
             }
 
             if (!preg_match('{^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*$}i', $information['name'])) {
-                $context->addViolationAt($property, 'The package name '.$information['name'].' is invalid, it should have a vendor name, a forward slash, and a package name. The vendor and package name can be words separated by -, . or _. The complete name should match "[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*".', array(), null);
+                $context->buildViolation('The package name '.$information['name'].' is invalid, it should have a vendor name, a forward slash, and a package name. The vendor and package name can be words separated by -, . or _. The complete name should match "[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*".')
+                    ->atPath($property)
+                    ->addViolation()
+                ;
                 return;
             }
 
             if (preg_match('{\.json$}', $information['name'])) {
-                $context->addViolationAt($property, 'The package name '.$information['name'].' is invalid, package names can not end in .json, consider renaming it or perhaps using a -json suffix instead.', array(), null);
+                $context->buildViolation('The package name '.$information['name'].' is invalid, package names can not end in .json, consider renaming it or perhaps using a -json suffix instead.')
+                    ->atPath($property)
+                    ->addViolation()
+                ;
                 return;
             }
 
@@ -253,14 +274,23 @@ class Package
                 $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $information['name']);
                 $suggestName = strtolower($suggestName);
 
-                $context->addViolationAt($property, 'The package name '.$information['name'].' is invalid, it should not contain uppercase characters. We suggest using '.$suggestName.' instead.');
+                $context->buildViolation('The package name '.$information['name'].' is invalid, it should not contain uppercase characters. We suggest using '.$suggestName.' instead.')
+                    ->atPath($property)
+                    ->addViolation()
+                ;
                 return;
             }
         } catch (\Exception $e) {
-            $context->addViolationAt($property, 'We had problems parsing your composer.json file, the parser reports: '.$e->getMessage(), array(), null);
+            $context->buildViolation('We had problems parsing your composer.json file, the parser reports: '.$e->getMessage())
+                ->atPath($property)
+                ->addViolation()
+            ;
         }
         if (null === $this->getName()) {
-            $context->addViolationAt($property, 'An unexpected error has made our parser fail to find a package name in your repository, if you think this is incorrect please try again', array(), null);
+            $context->buildViolation('An unexpected error has made our parser fail to find a package name in your repository, if you think this is incorrect please try again')
+                ->atPath($property)
+                ->addViolation()
+            ;
         }
     }
 
@@ -278,7 +308,10 @@ class Package
     {
         try {
             if ($this->entityRepository->findOneByName($this->name)) {
-                $context->addViolationAt('repository', 'A package with the name <a href="'.$this->router->generate('view_package', array('name' => $this->name)).'">'.$this->name.'</a> already exists.', array(), null);
+                $context->buildViolation('A package with the name <a href="'.$this->router->generate('view_package', array('name' => $this->name)).'">'.$this->name.'</a> already exists.')
+                    ->atPath('repository')
+                    ->addViolation()
+                ;
             }
         } catch (\Doctrine\ORM\NoResultException $e) {}
     }
@@ -288,15 +321,13 @@ class Package
         try {
             $vendor = $this->getVendor();
             if ($vendor && $this->entityRepository->isVendorTaken($vendor, reset($this->maintainers))) {
-                $context->addViolationAt(
-                    'repository',
-                    'The vendor is already taken by someone else. '
+                $context->buildViolation('The vendor is already taken by someone else. '
                         . 'You may ask them to add your package and give you maintainership access. '
                         . 'The packages already in that vendor namespace can be found at '
-                        . '<a href="'.$this->router->generate('view_vendor', array('vendor' => $vendor)).'">'.$vendor.'</a>',
-                    array(),
-                    null
-                );
+                        . '<a href="'.$this->router->generate('view_vendor', array('vendor' => $vendor)).'">'.$vendor.'</a>')
+                    ->atPath('repository')
+                    ->addViolation()
+                ;
             }
         } catch (\Doctrine\ORM\NoResultException $e) {}
     }