Skip to content

Commit

Permalink
Upgrade jms/metadata to 2.0
Browse files Browse the repository at this point in the history
 * Up minimum php version to 7.2
 * Used ::class instead of FQCN string
 * phpcsfixer
 * update travis
 * fix some tests deprecations
  • Loading branch information
Johann Untereiner committed Feb 16, 2019
1 parent 5b5a792 commit 8c254a4
Show file tree
Hide file tree
Showing 46 changed files with 186 additions and 159 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 7.1
- 7.2
- 7.3
- nightly
Expand All @@ -18,10 +17,8 @@ env:

matrix:
include:
- php: 7.1
- php: 7.2
env: VALIDATE_DOCS=true COMPOSER_FLAGS="--prefer-lowest"
- php: 7.1
env: SYMFONY_VERSION="~4.1.0" WITH_LIIP_IMAGINE=true
- php: 7.2
env: SYMFONY_VERSION="~4.2.0" WITH_LIIP_IMAGINE=true
- php: 7.3
Expand Down
6 changes: 3 additions & 3 deletions Command/MappingDebugClassCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$fqcn = $input->getArgument('fqcn');

if (!$this->metadataReader->isUploadable($fqcn)) {
$output->writeln(sprintf('<error>"%s" is not uploadable.</error>', $fqcn));
$output->writeln(\sprintf('<error>"%s" is not uploadable.</error>', $fqcn));

return 1;
}

$uploadableFields = $this->metadataReader->getUploadableFields($fqcn);

$output->writeln(sprintf('Introspecting class <info>%s</info>:', $fqcn));
$output->writeln(\sprintf('Introspecting class <info>%s</info>:', $fqcn));
foreach ($uploadableFields as $data) {
$output->writeln(sprintf('Found field "<comment>%s</comment>", storing file name in <comment>"%s</comment>" and using mapping "<comment>%s</comment>"', $data['propertyName'], $data['fileNameProperty'], $data['mapping']));
$output->writeln(\sprintf('Found field "<comment>%s</comment>", storing file name in <comment>"%s</comment>" and using mapping "<comment>%s</comment>"', $data['propertyName'], $data['fileNameProperty'], $data['mapping']));
}

return 0;
Expand Down
6 changes: 3 additions & 3 deletions Command/MappingDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ protected function execute(InputInterface $input, OutputInterface $output): void
$mapping = $input->getArgument('mapping');

if (!isset($this->mappings[$mapping])) {
throw new MappingNotFoundException(sprintf('Mapping "%s" does not exist.', $mapping));
throw new MappingNotFoundException(\sprintf('Mapping "%s" does not exist.', $mapping));
}

$output->writeln(sprintf('Debug information for mapping <info>%s</info>', $mapping));
$output->writeln(\sprintf('Debug information for mapping <info>%s</info>', $mapping));

foreach ($this->mappings[$mapping] as $key => $value) {
$output->writeln(sprintf('<comment>%s</comment>: %s', $key, var_export($value, true)));
$output->writeln(\sprintf('<comment>%s</comment>: %s', $key, \var_export($value, true)));
}
}
}
4 changes: 2 additions & 2 deletions Command/MappingListClassesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ protected function execute(InputInterface $input, OutputInterface $output): void
$uploadableClasses = $this->metadataReader->getUploadableClasses();

foreach ($uploadableClasses as $class) {
$output->writeln(sprintf('Found <comment>%s</comment>', $class));
$output->writeln(\sprintf('Found <comment>%s</comment>', $class));
}

$output->writeln(sprintf('Found <comment>%d</comment> classes.', \count($uploadableClasses)));
$output->writeln(\sprintf('Found <comment>%d</comment> classes.', \count($uploadableClasses)));
$output->writeln('<info>NOTE:</info> Only classes configured using XML or YAML are displayed.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function process(ContainerBuilder $container): void
$drivers[] = new Reference('vich_uploader.metadata_driver.annotation');
}

if (class_exists(Yaml::class)) {
if (\class_exists(Yaml::class)) {
$drivers[] = new Reference('vich_uploader.metadata_driver.yaml');
}

Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/Compiler/RegisterPropelModelsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public function process(ContainerBuilder $container): void
}

foreach ($serviceTypes as $type) {
if (!$container->has(sprintf('vich_uploader.listener.%s.%s', $type, $field['mapping']))) {
if (!$container->has(\sprintf('vich_uploader.listener.%s.%s', $type, $field['mapping']))) {
continue;
}

$definition = $container->getDefinition(sprintf('vich_uploader.listener.%s.%s', $type, $field['mapping']));
$definition = $container->getDefinition(\sprintf('vich_uploader.listener.%s.%s', $type, $field['mapping']));
$definition->setClass($container->getDefinition($definition->getParent())->getClass());
$definition->setPublic(true);
$definition->addTag('propel.event_subscriber', ['class' => $class]);
Expand Down
26 changes: 13 additions & 13 deletions DependencyInjection/VichUploaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('vich_uploader.default_filename_attribute_suffix', $config['default_filename_attribute_suffix']);
$container->setParameter('vich_uploader.mappings', $config['mappings']);

if (0 === strpos($config['storage'], '@')) {
$container->setAlias('vich_uploader.storage', substr($config['storage'], 1));
if (0 === \strpos($config['storage'], '@')) {
$container->setAlias('vich_uploader.storage', \substr($config['storage'], 1));
} else {
$container->setAlias('vich_uploader.storage', 'vich_uploader.storage.'.$config['storage']);
}
Expand Down Expand Up @@ -93,7 +93,7 @@ protected function registerMetadataDirectories(ContainerBuilder $container, arra
$ref = new \ReflectionClass($class);
$directory = \dirname($ref->getFileName()).'/Resources/config/vich_uploader';

if (!is_dir($directory)) {
if (!\is_dir($directory)) {
continue;
}

Expand All @@ -102,20 +102,20 @@ protected function registerMetadataDirectories(ContainerBuilder $container, arra
}

foreach ($config['metadata']['directories'] as $directory) {
$directory['path'] = rtrim(str_replace('\\', '/', $directory['path']), '/');
$directory['path'] = \rtrim(\str_replace('\\', '/', $directory['path']), '/');

if ('@' === $directory['path'][0]) {
$bundleName = substr($directory['path'], 1, strpos($directory['path'], '/') - 1);
$bundleName = \substr($directory['path'], 1, \strpos($directory['path'], '/') - 1);

if (!isset($bundles[$bundleName])) {
throw new \RuntimeException(sprintf('The bundle "%s" has not been registered with AppKernel. Available bundles: %s', $bundleName, implode(', ', array_keys($bundles))));
throw new \RuntimeException(\sprintf('The bundle "%s" has not been registered with AppKernel. Available bundles: %s', $bundleName, \implode(', ', \array_keys($bundles))));
}

$ref = new \ReflectionClass($bundles[$bundleName]);
$directory['path'] = \dirname($ref->getFileName()).substr($directory['path'], \strlen('@'.$bundleName));
$directory['path'] = \dirname($ref->getFileName()).\substr($directory['path'], \strlen('@'.$bundleName));
}

$directories[rtrim($directory['namespace_prefix'], '\\')] = rtrim($directory['path'], '\\/');
$directories[\rtrim($directory['namespace_prefix'], '\\')] = \rtrim($directory['path'], '\\/');
}

$container
Expand All @@ -135,8 +135,8 @@ protected function registerCacheStrategy(ContainerBuilder $container, array $con
;

$dir = $container->getParameterBag()->resolveValue($config['metadata']['file_cache']['dir']);
if (!file_exists($dir) && !@mkdir($dir, 0777, true)) {
throw new \RuntimeException(sprintf('Could not create cache directory "%s".', $dir));
if (!\file_exists($dir) && !@\mkdir($dir, 0777, true)) {
throw new \RuntimeException(\sprintf('Could not create cache directory "%s".', $dir));
}
} else {
$container->setAlias('vich_uploader.metadata.cache', new Alias($config['metadata']['cache'], false));
Expand Down Expand Up @@ -191,7 +191,7 @@ protected function createNamerServices(ContainerBuilder $container, array $confi

protected function createNamerService(ContainerBuilder $container, string $mappingName, array $mapping): array
{
$serviceId = sprintf('%s.%s', $mapping['namer']['service'], $mappingName);
$serviceId = \sprintf('%s.%s', $mapping['namer']['service'], $mappingName);
$container->setDefinition(
$serviceId, new ChildDefinition($mapping['namer']['service'])
);
Expand All @@ -209,7 +209,7 @@ protected function createListener(
int $priority = 0
): void {
$definition = $container
->setDefinition(sprintf('vich_uploader.listener.%s.%s', $type, $name), new ChildDefinition(sprintf('vich_uploader.listener.%s.%s', $type, $driver)))
->setDefinition(\sprintf('vich_uploader.listener.%s.%s', $type, $name), new ChildDefinition(\sprintf('vich_uploader.listener.%s.%s', $type, $driver)))
->replaceArgument(0, $name)
->replaceArgument(1, new Reference('vich_uploader.adapter.'.$driver));

Expand All @@ -224,7 +224,7 @@ private function registerFormTheme(ContainerBuilder $container): void
$resources = $container->hasParameter('twig.form.resources') ?
$container->getParameter('twig.form.resources') : [];

array_unshift($resources, '@VichUploader/Form/fields.html.twig');
\array_unshift($resources, '@VichUploader/Form/fields.html.twig');
$container->setParameter('twig.form.resources', $resources);
}
}
4 changes: 2 additions & 2 deletions Form/Type/VichFileType.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function configureOptions(OptionsResolver $resolver): void

$downloadUriNormalizer = function (Options $options, $downloadUri) {
if (null !== $options['download_link']) {
@trigger_error('The "download_link" option is deprecated since version 1.6 and will be removed in 2.0. You should use "download_uri" instead.', E_USER_DEPRECATED);
@\trigger_error('The "download_link" option is deprecated since version 1.6 and will be removed in 2.0. You should use "download_uri" instead.', E_USER_DEPRECATED);

return $options['download_link'];
}
Expand Down Expand Up @@ -153,7 +153,7 @@ public function buildView(FormView $view, FormInterface $form, array $options):
$view->vars['download_uri'] = null;
if ($options['download_uri'] && $object) {
$view->vars['download_uri'] = $this->resolveUriOption($options['download_uri'], $object, $form);
$view->vars = array_replace(
$view->vars = \array_replace(
$view->vars,
$this->resolveDownloadLabel($options['download_label'], $object, $form)
);
Expand Down
2 changes: 1 addition & 1 deletion Form/Type/VichImageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function buildView(FormView $view, FormInterface $form, array $options):
$view->vars['image_uri'] = $this->resolveUriOption($options['image_uri'], $object, $form);
}

$view->vars = array_replace(
$view->vars = \array_replace(
$view->vars,
$this->resolveDownloadLabel($options['download_label'], $object, $form)
);
Expand Down
7 changes: 4 additions & 3 deletions Metadata/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Vich\UploaderBundle\Metadata\Driver;

use Doctrine\Common\Annotations\Reader as AnnotationReader;
use Metadata\ClassMetadata as JMSClassMetadata;
use Metadata\Driver\AdvancedDriverInterface;
use Vich\UploaderBundle\Mapping\Annotation\Uploadable;
use Vich\UploaderBundle\Mapping\Annotation\UploadableField;
Expand Down Expand Up @@ -31,10 +32,10 @@ public function __construct(AnnotationReader $reader)
$this->reader = $reader;
}

public function loadMetadataForClass(\ReflectionClass $class)
public function loadMetadataForClass(\ReflectionClass $class): ?JMSClassMetadata
{
if (!$this->isUploadable($class)) {
return;
return null;
}

$classMetadata = new ClassMetadata($class->name);
Expand Down Expand Up @@ -65,7 +66,7 @@ public function loadMetadataForClass(\ReflectionClass $class)
return $classMetadata;
}

public function getAllClassNames()
public function getAllClassNames(): array
{
return [];
}
Expand Down
9 changes: 5 additions & 4 deletions Metadata/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vich\UploaderBundle\Metadata\Driver;

use Metadata\ClassMetadata as JMSClassMetadata;
use Metadata\Driver\AbstractFileDriver;
use Symfony\Component\Config\Util\XmlUtils;
use Vich\UploaderBundle\Metadata\ClassMetadata;
Expand All @@ -12,10 +13,10 @@
*/
class XmlDriver extends AbstractFileDriver
{
protected function loadMetadataFromFile(\ReflectionClass $class, $file)
protected function loadMetadataFromFile(\ReflectionClass $class, string $file): ?JMSClassMetadata
{
$elem = XmlUtils::loadFile($file);
$elem = simplexml_import_dom($elem);
$elem = \simplexml_import_dom($elem);

$className = $this->guessClassName($file, $elem, $class);
$classMetadata = new ClassMetadata($className);
Expand All @@ -42,7 +43,7 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $file)
/**
* {@inheritdoc}
*/
protected function getExtension()
protected function getExtension(): string
{
return 'xml';
}
Expand All @@ -54,7 +55,7 @@ protected function guessClassName($file, \SimpleXMLElement $elem, \ReflectionCla
}

if ($class->name !== (string) $elem->attributes()->class) {
throw new \RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $class->name, $file));
throw new \RuntimeException(\sprintf('Expected metadata for class %s to be defined in %s.', $class->name, $file));
}

return $class->name;
Expand Down
21 changes: 11 additions & 10 deletions Metadata/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vich\UploaderBundle\Metadata\Driver;

use Metadata\ClassMetadata as JMSClassMetadata;
use Metadata\Driver\AbstractFileDriver;
use Symfony\Component\Yaml\Yaml as YmlParser;
use Vich\UploaderBundle\Metadata\ClassMetadata;
Expand All @@ -15,7 +16,7 @@ class YamlDriver extends AbstractFileDriver
/**
* {@inheritdoc}
*/
protected function loadMetadataFromFile(\ReflectionClass $class, $file)
protected function loadMetadataFromFile(\ReflectionClass $class, string $file): ?JMSClassMetadata
{
$config = $this->loadMappingFile($file);
$className = $this->guessClassName($file, $config, $class);
Expand All @@ -27,11 +28,11 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $file)
$fieldMetadata = [
'mapping' => $mappingData['mapping'],
'propertyName' => $field,
'fileNameProperty' => isset($mappingData['filename_property']) ? $mappingData['filename_property'] : null,
'size' => isset($mappingData['size']) ? $mappingData['size'] : null,
'mimeType' => isset($mappingData['mime_type']) ? $mappingData['mime_type'] : null,
'originalName' => isset($mappingData['original_name']) ? $mappingData['original_name'] : null,
'dimensions' => isset($mappingData['dimensions']) ? $mappingData['dimensions'] : null,
'fileNameProperty' => $mappingData['filename_property'] ?? null,
'size' => $mappingData['size'] ?? null,
'mimeType' => $mappingData['mime_type'] ?? null,
'originalName' => $mappingData['original_name'] ?? null,
'dimensions' => $mappingData['dimensions'] ?? null,
];

$classMetadata->fields[$field] = $fieldMetadata;
Expand All @@ -42,25 +43,25 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $file)

protected function loadMappingFile($file)
{
return YmlParser::parse(file_get_contents($file));
return YmlParser::parse(\file_get_contents($file));
}

/**
* {@inheritdoc}
*/
protected function getExtension()
protected function getExtension(): string
{
return 'yml';
}

protected function guessClassName($file, array $config, \ReflectionClass $class = null)
{
if (null === $class) {
return current(array_keys($config));
return \current(\array_keys($config));
}

if (!isset($config[$class->name])) {
throw new \RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $class->name, $file));
throw new \RuntimeException(\sprintf('Expected metadata for class %s to be defined in %s.', $class->name, $file));
}

return $class->name;
Expand Down
5 changes: 3 additions & 2 deletions Tests/Adapter/ODM/MongoDB/MongoDBAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vich\UploaderBundle\Tests\Adapter\ODM\MongoDB;

use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
use PHPUnit\Framework\TestCase;
use Vich\UploaderBundle\Adapter\ODM\MongoDB\MongoDBAdapter;
use Vich\UploaderBundle\Tests\DummyEntity;
Expand All @@ -15,7 +16,7 @@ class MongoDBAdapterTest extends TestCase
{
public static function setUpBeforeClass(): void
{
if (!class_exists('Doctrine\ODM\MongoDB\Event\LifecycleEventArgs')) {
if (!\class_exists(LifecycleEventArgs::class)) {
self::markTestSkipped('Doctrine\ODM\MongoDB\Event\LifecycleEventArgs does not exist.');
}
}
Expand All @@ -27,7 +28,7 @@ public function testGetObjectFromArgs(): void
{
$entity = new DummyEntity();

$args = $this->getMockBuilder('Doctrine\ODM\MongoDB\Event\LifecycleEventArgs')
$args = $this->getMockBuilder(LifecycleEventArgs::class)
->disableOriginalConstructor()
->getMock();
$args
Expand Down
5 changes: 3 additions & 2 deletions Tests/Adapter/ORM/DoctrineORMAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vich\UploaderBundle\Tests\Adapter\ORM;

use Doctrine\ORM\Event\LifecycleEventArgs;
use PHPUnit\Framework\TestCase;
use Vich\UploaderBundle\Adapter\ORM\DoctrineORMAdapter;
use Vich\UploaderBundle\Tests\DummyEntity;
Expand All @@ -15,7 +16,7 @@ class DoctrineORMAdapterTest extends TestCase
{
public static function setUpBeforeClass(): void
{
if (!class_exists('Doctrine\ORM\Event\LifecycleEventArgs')) {
if (!\class_exists(LifecycleEventArgs::class)) {
self::markTestSkipped('Doctrine\ORM\Event\LifecycleEventArgs does not exist.');
}
}
Expand All @@ -27,7 +28,7 @@ public function testGetObjectFromArgs(): void
{
$entity = new DummyEntity();

$args = $this->getMockBuilder('Doctrine\ORM\Event\LifecycleEventArgs')
$args = $this->getMockBuilder(LifecycleEventArgs::class)
->disableOriginalConstructor()
->getMock();
$args
Expand Down
2 changes: 1 addition & 1 deletion Tests/Adapter/PHPCR/PHPCRAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PHPCRAdapterTest extends TestCase
{
public static function setUpBeforeClass(): void
{
if (!class_exists(LifecycleEventArgs::class)) {
if (!\class_exists(LifecycleEventArgs::class)) {
self::markTestSkipped('Doctrine\Common\Persistence\Event\LifecycleEventArgs does not exist.');
}
}
Expand Down
Loading

0 comments on commit 8c254a4

Please sign in to comment.