From 2a39e12d078fb108a937f054b3b617c3a14f3a49 Mon Sep 17 00:00:00 2001 From: Massimiliano Arione Date: Mon, 25 Dec 2017 11:27:15 +0100 Subject: [PATCH] increase requirements --- .php_cs | 5 ++ .travis.yml | 13 +-- Adapter/ODM/MongoDB/MongoDBAdapter.php | 2 +- Adapter/ORM/DoctrineORMAdapter.php | 2 +- Adapter/PHPCR/PHPCRAdapter.php | 2 +- Adapter/Propel/PropelORMAdapter.php | 2 +- Command/MappingDebugClassCommand.php | 3 +- Command/MappingDebugCommand.php | 4 +- Command/MappingListClassesCommand.php | 4 +- .../Compiler/RegisterPropelModelsPass.php | 2 +- DependencyInjection/Configuration.php | 11 +-- DependencyInjection/VichUploaderExtension.php | 42 +++------ Entity/File.php | 50 +++-------- Event/Event.php | 4 +- Event/Events.php | 12 +-- EventListener/Doctrine/BaseListener.php | 22 ++--- EventListener/Doctrine/CleanListener.php | 4 +- EventListener/Doctrine/InjectListener.php | 6 +- EventListener/Doctrine/RemoveListener.php | 8 +- EventListener/Doctrine/UploadListener.php | 10 ++- EventListener/Propel/BaseListener.php | 16 ++-- EventListener/Propel/CleanListener.php | 6 +- EventListener/Propel/InjectListener.php | 6 +- EventListener/Propel/RemoveListener.php | 6 +- EventListener/Propel/UploadListener.php | 6 +- Exception/MappingNotFoundException.php | 2 +- Form/DataTransformer/FileTransformer.php | 2 +- Form/Type/VichFileType.php | 36 +++----- Form/Type/VichImageType.php | 16 ++-- Handler/AbstractHandler.php | 8 +- Handler/DownloadHandler.php | 18 ++-- Handler/UploadHandler.php | 22 ++--- Injector/FileInjector.php | 10 +-- Injector/FileInjectorInterface.php | 2 +- Mapping/PropertyMapping.php | 85 ++++++++++++------- Mapping/PropertyMappingFactory.php | 38 +++++---- Metadata/ClassMetadata.php | 8 +- Metadata/MetadataReader.php | 22 +++-- Naming/Base64Namer.php | 11 +-- Naming/DirectoryNamerInterface.php | 2 +- Naming/HashNamer.php | 11 +-- Naming/NamerInterface.php | 2 +- Naming/OrignameNamer.php | 7 +- Naming/Polyfill/FileExtensionTrait.php | 4 +- Naming/PropertyDirectoryNamer.php | 12 +-- Naming/PropertyNamer.php | 9 +- Naming/SubdirDirectoryNamer.php | 7 +- Naming/UniqidNamer.php | 5 +- Storage/AbstractStorage.php | 85 ++++++------------- Storage/FileSystemStorage.php | 26 ++---- Storage/FlysystemStorage.php | 42 +++------ Storage/GaufretteStorage.php | 21 ++--- Storage/StorageInterface.php | 13 ++- Templating/Helper/UploaderHelper.php | 14 +-- .../ODM/MongoDB/MongoDBAdapterTest.php | 4 +- Tests/Adapter/ORM/DoctrineORMAdapterTest.php | 4 +- Tests/Adapter/PHPCR/PHPCRAdapterTest.php | 4 +- Tests/Adapter/Propel/PropelORMAdapterTest.php | 8 +- .../VichUploaderExtensionTest.php | 16 ++-- Tests/DummyEntity.php | 6 +- .../Doctrine/CleanListenerTest.php | 8 +- .../Doctrine/InjectListenerTest.php | 8 +- .../Doctrine/ListenerTestCase.php | 2 +- .../Doctrine/RemoveListenerTest.php | 12 +-- .../Doctrine/UploadListenerTest.php | 12 +-- .../Propel/CleanListenerTest.php | 6 +- .../Propel/InjectListenerTest.php | 6 +- .../EventListener/Propel/ListenerTestCase.php | 2 +- .../Propel/RemoveListenerTest.php | 6 +- .../Propel/UploadListenerTest.php | 6 +- Tests/Fixtures/App/app/AppKernel.php | 14 +-- .../App/src/TestBundle/Entity/Article.php | 18 ++-- .../App/src/TestBundle/Entity/Image.php | 6 +- .../App/src/TestBundle/Entity/Product.php | 12 +-- Tests/Form/Type/VichFileTypeTest.php | 10 +-- Tests/Form/Type/VichImageTypeTest.php | 13 ++- Tests/Functional/UploadTest.php | 4 +- Tests/Functional/WebTestCase.php | 2 +- Tests/Handler/DownloadHandlerTest.php | 24 +++--- Tests/Handler/UploadHandlerTest.php | 23 ++--- Tests/Injector/FileInjectorTest.php | 6 +- .../Annotation/UploadableFieldTest.php | 7 +- Tests/Mapping/PropertyMappingFactoryTest.php | 35 ++++---- Tests/Mapping/PropertyMappingTest.php | 72 +++------------- Tests/Metadata/ClassMetadataTest.php | 2 +- .../Metadata/Driver/AnnotationDriverTest.php | 8 +- Tests/Metadata/Driver/FileDriverTestCase.php | 2 +- Tests/Metadata/Driver/YamlDriverTest.php | 7 +- Tests/Metadata/MetadataReaderTest.php | 34 ++++---- Tests/Naming/Base64NamerTest.php | 6 +- Tests/Naming/HashNamerTest.php | 4 +- Tests/Naming/OrignameNamerTest.php | 2 +- Tests/Naming/PropertyDirectoryNamerTest.php | 34 ++++---- Tests/Naming/PropertyNamerTest.php | 34 ++++---- Tests/Naming/SubdirDirectoryNamerTest.php | 2 +- Tests/Naming/UniqidNamerTest.php | 2 +- Tests/Storage/FileSystemStorageTest.php | 48 ++--------- Tests/Storage/FlysystemStorageTest.php | 12 +-- Tests/Storage/GaufretteStorageTest.php | 48 ++--------- Tests/Storage/StorageTestCase.php | 21 +---- Tests/Templating/Helper/UploadHelperTest.php | 6 +- .../Twig/Extension/UploaderExtensionTest.php | 11 +-- Tests/Util/FilenameUtilsTest.php | 2 +- Twig/Extension/UploaderExtension.php | 27 +----- Util/ClassUtils.php | 2 +- Util/FilenameUtils.php | 2 +- Util/Transliterator.php | 4 +- VichUploaderBundle.php | 2 +- composer.json | 49 +++++------ 109 files changed, 601 insertions(+), 914 deletions(-) diff --git a/.php_cs b/.php_cs index 794906a7..cba76c2c 100644 --- a/.php_cs +++ b/.php_cs @@ -6,10 +6,15 @@ $finder = PhpCsFixer\Finder::create() ; return PhpCsFixer\Config::create() + ->setRiskyAllowed(true) ->setRules([ '@Symfony' => true, + '@Symfony:risky' => true, + '@PHP71Migration:risky' => true, + '@PHPUnit60Migration:risky' => true, 'array_syntax' => ['syntax' => 'short'], 'ordered_imports' => true, + 'declare_strict_types' => false, ]) ->setFinder($finder) ; diff --git a/.travis.yml b/.travis.yml index 3d02be19..df6a777b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 7.0 - 7.1 - 7.2 - nightly @@ -13,21 +12,17 @@ cache: - $HOME/.composer/cache env: - - SYMFONY_VERSION="~2.8.0" + - SYMFONY_VERSION="~3.4.0" + - COMPOSER_FLAGS="--prefer-dist" matrix: include: - php: 7.1 env: VALIDATE_DOCS=true - - php: 7.0 env: COMPOSER_FLAGS="--prefer-lowest" - php: 7.1 - env: SYMFONY_VERSION="~3.3.0" + env: SYMFONY_VERSION="~3.4.0" WITH_LIIP_IMAGINE=true - php: 7.2 - env: SYMFONY_VERSION="~3.4.0" - - php: 7.1 - env: SYMFONY_VERSION="~2.8.0" WITH_LIIP_IMAGINE=true - - php: 7.1 env: SYMFONY_VERSION="~4.0.0" WITH_LIIP_IMAGINE=true allow_failures: - php: nightly @@ -40,7 +35,7 @@ before_install: - if [ "$WITH_LIIP_IMAGINE" = true ] ; then composer require --no-update liip/imagine-bundle:"^1.7|^2.0@dev"; fi; - if [ "$VALIDATE_DOCS" = true ]; then composer require --dev --no-update kphoen/rusty dev-master; fi -install: php -d memory_limit=-1 $(phpenv which composer) update $COMPOSER_FLAGS --no-suggest --prefer-dist +install: php -d memory_limit=-1 $(phpenv which composer) update $COMPOSER_FLAGS --no-suggest script: - if [ "$VALIDATE_DOCS" != true ]; then ./vendor/bin/phpunit; fi diff --git a/Adapter/ODM/MongoDB/MongoDBAdapter.php b/Adapter/ODM/MongoDB/MongoDBAdapter.php index c02dc43f..1667fcc6 100644 --- a/Adapter/ODM/MongoDB/MongoDBAdapter.php +++ b/Adapter/ODM/MongoDB/MongoDBAdapter.php @@ -22,7 +22,7 @@ public function getObjectFromArgs($event) /** * {@inheritdoc} */ - public function recomputeChangeSet($event) + public function recomputeChangeSet($event): void { $object = $this->getObjectFromArgs($event); diff --git a/Adapter/ORM/DoctrineORMAdapter.php b/Adapter/ORM/DoctrineORMAdapter.php index 49e1d0e7..d61342b2 100644 --- a/Adapter/ORM/DoctrineORMAdapter.php +++ b/Adapter/ORM/DoctrineORMAdapter.php @@ -22,7 +22,7 @@ public function getObjectFromArgs($event) /** * {@inheritdoc} */ - public function recomputeChangeSet($event) + public function recomputeChangeSet($event): void { $object = $this->getObjectFromArgs($event); diff --git a/Adapter/PHPCR/PHPCRAdapter.php b/Adapter/PHPCR/PHPCRAdapter.php index f0d62008..fa88e02a 100644 --- a/Adapter/PHPCR/PHPCRAdapter.php +++ b/Adapter/PHPCR/PHPCRAdapter.php @@ -20,7 +20,7 @@ public function getObjectFromArgs($event) /** * {@inheritdoc} */ - public function recomputeChangeSet($event) + public function recomputeChangeSet($event): void { $object = $this->getObjectFromArgs($event); diff --git a/Adapter/Propel/PropelORMAdapter.php b/Adapter/Propel/PropelORMAdapter.php index c92a4c82..b0686620 100644 --- a/Adapter/Propel/PropelORMAdapter.php +++ b/Adapter/Propel/PropelORMAdapter.php @@ -22,7 +22,7 @@ public function getObjectFromArgs($event) /** * {@inheritdoc} */ - public function recomputeChangeSet($event) + public function recomputeChangeSet($event): void { } } diff --git a/Command/MappingDebugClassCommand.php b/Command/MappingDebugClassCommand.php index 60f6d56b..aec18d01 100644 --- a/Command/MappingDebugClassCommand.php +++ b/Command/MappingDebugClassCommand.php @@ -6,6 +6,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Vich\UploaderBundle\Metadata\MetadataReader; class MappingDebugClassCommand extends Command { @@ -19,7 +20,7 @@ public function __construct(MetadataReader $metadataReader) $this->metadataReader = $metadataReader; } - protected function configure() + protected function configure(): void { $this ->setName('vich:mapping:debug-class') diff --git a/Command/MappingDebugCommand.php b/Command/MappingDebugCommand.php index 0e8f53fc..30c991e3 100644 --- a/Command/MappingDebugCommand.php +++ b/Command/MappingDebugCommand.php @@ -20,7 +20,7 @@ public function __construct(array $mappings) $this->mappings = $mappings; } - protected function configure() + protected function configure(): void { $this ->setName('vich:mapping:debug') @@ -29,7 +29,7 @@ protected function configure() ; } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): void { $mapping = $input->getArgument('mapping'); diff --git a/Command/MappingListClassesCommand.php b/Command/MappingListClassesCommand.php index 8e93863d..1db9cbc7 100644 --- a/Command/MappingListClassesCommand.php +++ b/Command/MappingListClassesCommand.php @@ -19,7 +19,7 @@ public function __construct(MetadataReader $metadataReader) $this->metadataReader = $metadataReader; } - protected function configure() + protected function configure(): void { $this ->setName('vich:mapping:list-classes') @@ -27,7 +27,7 @@ protected function configure() ; } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): void { $output->writeln('Looking for uploadable classes.'); diff --git a/DependencyInjection/Compiler/RegisterPropelModelsPass.php b/DependencyInjection/Compiler/RegisterPropelModelsPass.php index 5a44c1ad..e48d85d2 100644 --- a/DependencyInjection/Compiler/RegisterPropelModelsPass.php +++ b/DependencyInjection/Compiler/RegisterPropelModelsPass.php @@ -16,7 +16,7 @@ class RegisterPropelModelsPass implements CompilerPassInterface /** * {@inheritdoc} */ - public function process(ContainerBuilder $container) + public function process(ContainerBuilder $container): void { if (!$container->hasParameter('vich_uploader.mappings')) { return; diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 665dcfad..ced6e38b 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -17,10 +17,7 @@ class Configuration implements ConfigurationInterface protected $supportedStorages = ['gaufrette', 'flysystem', 'file_system']; - /** - * {@inheritdoc} - */ - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $tb = new TreeBuilder(); $root = $tb->root('vich_uploader'); @@ -32,7 +29,7 @@ public function getConfigTreeBuilder() return $tb; } - protected function addGeneralSection(ArrayNodeDefinition $node) + protected function addGeneralSection(ArrayNodeDefinition $node): void { $node ->children() @@ -74,7 +71,7 @@ protected function addGeneralSection(ArrayNodeDefinition $node) ; } - protected function addMetadataSection(ArrayNodeDefinition $node) + protected function addMetadataSection(ArrayNodeDefinition $node): void { $node ->children() @@ -103,7 +100,7 @@ protected function addMetadataSection(ArrayNodeDefinition $node) ->end(); } - protected function addMappingsSection(ArrayNodeDefinition $node) + protected function addMappingsSection(ArrayNodeDefinition $node): void { $node ->children() diff --git a/DependencyInjection/VichUploaderExtension.php b/DependencyInjection/VichUploaderExtension.php index deeb2396..23882344 100644 --- a/DependencyInjection/VichUploaderExtension.php +++ b/DependencyInjection/VichUploaderExtension.php @@ -6,10 +6,9 @@ use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\DefinitionDecorator; +use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Reference; -use Symfony\Component\DependencyInjection\Extension\Extension; use Vich\UploaderBundle\Templating\Helper\UploaderHelper; /** @@ -26,13 +25,7 @@ class VichUploaderExtension extends Extension 'phpcr' => 'doctrine_phpcr.event_subscriber', ]; - /** - * Loads the extension. - * - * @param array $configs The configuration - * @param ContainerBuilder $container The container builder - */ - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); @@ -59,7 +52,7 @@ public function load(array $configs, ContainerBuilder $container) $this->registerFormTheme($container); } - protected function loadServicesFiles(ContainerBuilder $container, array $config) + protected function loadServicesFiles(ContainerBuilder $container, array $config): void { $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); @@ -71,7 +64,7 @@ protected function loadServicesFiles(ContainerBuilder $container, array $config) $loader->load($file); } - if (in_array($config['storage'], ['gaufrette', 'flysystem'])) { + if (in_array($config['storage'], ['gaufrette', 'flysystem'], true)) { $loader->load($config['storage'].'.xml'); } @@ -87,7 +80,7 @@ protected function loadServicesFiles(ContainerBuilder $container, array $config) } } - protected function registerMetadataDirectories(ContainerBuilder $container, array $config) + protected function registerMetadataDirectories(ContainerBuilder $container, array $config): void { $bundles = $container->getParameter('kernel.bundles'); @@ -129,7 +122,7 @@ protected function registerMetadataDirectories(ContainerBuilder $container, arra ; } - protected function registerCacheStrategy(ContainerBuilder $container, array $config) + protected function registerCacheStrategy(ContainerBuilder $container, array $config): void { if ('none' === $config['metadata']['cache']) { $container->removeAlias('vich_uploader.metadata.cache'); @@ -148,7 +141,7 @@ protected function registerCacheStrategy(ContainerBuilder $container, array $con } } - protected function fixDbDriverConfig(array $config) + protected function fixDbDriverConfig(array $config): array { // mapping with no declared db_driver use the top-level one foreach ($config['mappings'] as &$mapping) { @@ -158,7 +151,7 @@ protected function fixDbDriverConfig(array $config) return $config; } - protected function registerListeners(ContainerBuilder $container, array $config) + protected function registerListeners(ContainerBuilder $container, array $config): void { $servicesMap = [ 'inject_on_load' => ['name' => 'inject', 'priority' => 0], @@ -183,7 +176,7 @@ protected function registerListeners(ContainerBuilder $container, array $config) } } - protected function createNamerServices(ContainerBuilder $container, array $config) + protected function createNamerServices(ContainerBuilder $container, array $config): array { foreach ($config['mappings'] as $name => $mapping) { if (!empty($mapping['namer']['service'])) { @@ -194,12 +187,11 @@ protected function createNamerServices(ContainerBuilder $container, array $confi return $config; } - protected function createNamerService(ContainerBuilder $container, $mappingName, array $mapping) + protected function createNamerService(ContainerBuilder $container, $mappingName, array $mapping): array { - $definitionClassname = $this->getDefinitionClassname(); $serviceId = sprintf('%s.%s', $mapping['namer']['service'], $mappingName); $container->setDefinition( - $serviceId, new $definitionClassname($mapping['namer']['service']) + $serviceId, new ChildDefinition($mapping['namer']['service']) ); $mapping['namer']['service'] = $serviceId; @@ -207,11 +199,10 @@ protected function createNamerService(ContainerBuilder $container, $mappingName, return $mapping; } - protected function createListener(ContainerBuilder $container, $name, $type, $driver, $priority = 0) + protected function createListener(ContainerBuilder $container, $name, $type, $driver, $priority = 0): void { - $definitionClassname = $this->getDefinitionClassname(); $definition = $container - ->setDefinition(sprintf('vich_uploader.listener.%s.%s', $type, $name), new $definitionClassname(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)); @@ -221,7 +212,7 @@ protected function createListener(ContainerBuilder $container, $name, $type, $dr } } - private function registerFormTheme(ContainerBuilder $container) + private function registerFormTheme(ContainerBuilder $container): void { $resources = $container->hasParameter('twig.form.resources') ? $container->getParameter('twig.form.resources') : []; @@ -229,9 +220,4 @@ private function registerFormTheme(ContainerBuilder $container) array_unshift($resources, '@VichUploader/Form/fields.html.twig'); $container->setParameter('twig.form.resources', $resources); } - - private function getDefinitionClassname(): string - { - return class_exists(ChildDefinition::class) ? ChildDefinition::class : DefinitionDecorator::class; - } } diff --git a/Entity/File.php b/Entity/File.php index 5d9c3fff..fac95778 100644 --- a/Entity/File.php +++ b/Entity/File.php @@ -34,82 +34,52 @@ class File */ protected $dimensions; - /** - * @return string|null - */ - public function getName() + public function getName(): ?string { return $this->name; } - /** - * @param string|null $name - */ - public function setName($name) + public function setName(?string $name): void { $this->name = $name; } - /** - * @return string|null - */ - public function getOriginalName() + public function getOriginalName(): ?string { return $this->originalName; } - /** - * @param string|null $originalName - */ - public function setOriginalName($originalName) + public function setOriginalName(?string $originalName): void { $this->originalName = $originalName; } - /** - * @return string|null - */ - public function getMimeType() + public function getMimeType(): ?string { return $this->mimeType; } - /** - * @param string|null $mimeType - */ - public function setMimeType($mimeType) + public function setMimeType(?string $mimeType): void { $this->mimeType = $mimeType; } - /** - * @return int|null - */ - public function getSize() + public function getSize(): ?int { return $this->size; } - /** - * @param int $size - */ - public function setSize($size) + public function setSize(?int $size): void { $this->size = $size; } - /** - * @return array|null - */ - public function getDimensions() + public function getDimensions(): ?array { return $this->dimensions; } - /** - * @param array $dimensions - */ - public function setDimensions($dimensions) + public function setDimensions(?array $dimensions): void { $this->dimensions = $dimensions; } diff --git a/Event/Event.php b/Event/Event.php index b57ef017..bb5e3254 100644 --- a/Event/Event.php +++ b/Event/Event.php @@ -25,7 +25,7 @@ public function __construct($object, PropertyMapping $mapping) /** * Accessor to the object being manipulated. * - * @return mixed + * @return object */ public function getObject() { @@ -37,7 +37,7 @@ public function getObject() * * @return PropertyMapping */ - public function getMapping() + public function getMapping(): PropertyMapping { return $this->mapping; } diff --git a/Event/Events.php b/Event/Events.php index 1f55913f..4bba080b 100644 --- a/Event/Events.php +++ b/Event/Events.php @@ -16,7 +16,7 @@ final class Events * * @Event("Vich\UploaderBundle\Event\Event") */ - const PRE_UPLOAD = 'vich_uploader.pre_upload'; + public const PRE_UPLOAD = 'vich_uploader.pre_upload'; /** * Triggered right after a file upload is handled. @@ -25,33 +25,33 @@ final class Events * * @Event("Vich\UploaderBundle\Event\Event") */ - const POST_UPLOAD = 'vich_uploader.post_upload'; + public const POST_UPLOAD = 'vich_uploader.post_upload'; /** * Triggered before a file is injected into an entity. * * @Event("Vich\UploaderBundle\Event\Event") */ - const PRE_INJECT = 'vich_uploader.pre_inject'; + public const PRE_INJECT = 'vich_uploader.pre_inject'; /** * Triggered after a file is injected into an entity. * * @Event("Vich\UploaderBundle\Event\Event") */ - const POST_INJECT = 'vich_uploader.post_inject'; + public const POST_INJECT = 'vich_uploader.post_inject'; /** * Triggered before a file is removed. * * @Event("Vich\UploaderBundle\Event\Event") */ - const PRE_REMOVE = 'vich_uploader.pre_remove'; + public const PRE_REMOVE = 'vich_uploader.pre_remove'; /** * Triggered after a file is removed. * * @Event("Vich\UploaderBundle\Event\Event") */ - const POST_REMOVE = 'vich_uploader.post_remove'; + public const POST_REMOVE = 'vich_uploader.post_remove'; } diff --git a/EventListener/Doctrine/BaseListener.php b/EventListener/Doctrine/BaseListener.php index 0fee831a..5c59995f 100644 --- a/EventListener/Doctrine/BaseListener.php +++ b/EventListener/Doctrine/BaseListener.php @@ -35,15 +35,7 @@ abstract class BaseListener implements EventSubscriber */ protected $handler; - /** - * Constructs a new instance of UploaderListener. - * - * @param string $mapping The mapping name - * @param AdapterInterface $adapter The adapter - * @param MetadataReader $metadata The metadata reader - * @param UploadHandler $handler The upload handler - */ - public function __construct($mapping, AdapterInterface $adapter, MetadataReader $metadata, UploadHandler $handler) + public function __construct(string $mapping, AdapterInterface $adapter, MetadataReader $metadata, UploadHandler $handler) { $this->mapping = $mapping; $this->adapter = $adapter; @@ -54,11 +46,11 @@ public function __construct($mapping, AdapterInterface $adapter, MetadataReader /** * Checks if the given object is uploadable using the current mapping. * - * @param mixed $object The object to test + * @param object $object The object to test * * @return bool */ - protected function isUploadable($object) + protected function isUploadable($object): bool { return $this->metadata->isUploadable(ClassUtils::getClass($object), $this->mapping); } @@ -66,11 +58,13 @@ protected function isUploadable($object) /** * Returns a list of uploadable fields for the given object and mapping. * - * @param mixed $object The object to use + * @param object $object The object to use + * + * @return array|string[] A list of field names * - * @return string[] A list of field names + * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException */ - protected function getUploadableFields($object) + protected function getUploadableFields($object): array { $fields = $this->metadata->getUploadableFields(ClassUtils::getClass($object), $this->mapping); diff --git a/EventListener/Doctrine/CleanListener.php b/EventListener/Doctrine/CleanListener.php index 4d44b69f..2245e9d5 100644 --- a/EventListener/Doctrine/CleanListener.php +++ b/EventListener/Doctrine/CleanListener.php @@ -18,7 +18,7 @@ class CleanListener extends BaseListener * * @return array The array of events */ - public function getSubscribedEvents() + public function getSubscribedEvents(): array { return [ 'preUpdate', @@ -28,7 +28,7 @@ public function getSubscribedEvents() /** * @param EventArgs $event The event */ - public function preUpdate(EventArgs $event) + public function preUpdate(EventArgs $event): void { $object = $this->adapter->getObjectFromArgs($event); diff --git a/EventListener/Doctrine/InjectListener.php b/EventListener/Doctrine/InjectListener.php index 595b5b8a..2d8e022f 100644 --- a/EventListener/Doctrine/InjectListener.php +++ b/EventListener/Doctrine/InjectListener.php @@ -18,7 +18,7 @@ class InjectListener extends BaseListener * * @return array The array of events */ - public function getSubscribedEvents() + public function getSubscribedEvents(): array { return [ 'postLoad', @@ -27,8 +27,10 @@ public function getSubscribedEvents() /** * @param EventArgs $event The event + * + * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException */ - public function postLoad(EventArgs $event) + public function postLoad(EventArgs $event): void { $object = $this->adapter->getObjectFromArgs($event); diff --git a/EventListener/Doctrine/RemoveListener.php b/EventListener/Doctrine/RemoveListener.php index f31849ae..baf8475f 100644 --- a/EventListener/Doctrine/RemoveListener.php +++ b/EventListener/Doctrine/RemoveListener.php @@ -19,7 +19,7 @@ class RemoveListener extends BaseListener * * @return array The array of events */ - public function getSubscribedEvents() + public function getSubscribedEvents(): array { return [ 'preRemove', @@ -32,7 +32,7 @@ public function getSubscribedEvents() * * @param EventArgs $event The event */ - public function preRemove(EventArgs $event) + public function preRemove(EventArgs $event): void { $object = $this->adapter->getObjectFromArgs($event); @@ -43,8 +43,10 @@ public function preRemove(EventArgs $event) /** * @param EventArgs $event The event + * + * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException */ - public function postRemove(EventArgs $event) + public function postRemove(EventArgs $event): void { $object = $this->adapter->getObjectFromArgs($event); diff --git a/EventListener/Doctrine/UploadListener.php b/EventListener/Doctrine/UploadListener.php index f101cb88..131ac350 100644 --- a/EventListener/Doctrine/UploadListener.php +++ b/EventListener/Doctrine/UploadListener.php @@ -18,7 +18,7 @@ class UploadListener extends BaseListener * * @return array The array of events */ - public function getSubscribedEvents() + public function getSubscribedEvents(): array { return [ 'prePersist', @@ -28,8 +28,10 @@ public function getSubscribedEvents() /** * @param EventArgs $event The event + * + * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException */ - public function prePersist(EventArgs $event) + public function prePersist(EventArgs $event): void { $object = $this->adapter->getObjectFromArgs($event); @@ -44,8 +46,10 @@ public function prePersist(EventArgs $event) /** * @param EventArgs $event The event + * + * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException */ - public function preUpdate(EventArgs $event) + public function preUpdate(EventArgs $event): void { $object = $this->adapter->getObjectFromArgs($event); diff --git a/EventListener/Propel/BaseListener.php b/EventListener/Propel/BaseListener.php index 0bf8dee9..f5a44ae3 100644 --- a/EventListener/Propel/BaseListener.php +++ b/EventListener/Propel/BaseListener.php @@ -35,15 +35,7 @@ abstract class BaseListener implements EventSubscriberInterface */ protected $handler; - /** - * Constructs a new instance of BaseListener. - * - * @param string $mapping The mapping name - * @param AdapterInterface $adapter The adapter - * @param MetadataReader $metadata The metadata reader - * @param UploadHandler $handler The upload handler - */ - public function __construct($mapping, AdapterInterface $adapter, MetadataReader $metadata, UploadHandler $handler) + public function __construct(string $mapping, AdapterInterface $adapter, MetadataReader $metadata, UploadHandler $handler) { $this->mapping = $mapping; $this->adapter = $adapter; @@ -56,9 +48,11 @@ public function __construct($mapping, AdapterInterface $adapter, MetadataReader * * @param mixed $object The object to use * - * @return string[] A list of field names + * @return array|string[] A list of field names + * + * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException */ - protected function getUploadableFields($object) + protected function getUploadableFields($object): array { $fields = $this->metadata->getUploadableFields(ClassUtils::getClass($object), $this->mapping); diff --git a/EventListener/Propel/CleanListener.php b/EventListener/Propel/CleanListener.php index c18a2fec..20952745 100644 --- a/EventListener/Propel/CleanListener.php +++ b/EventListener/Propel/CleanListener.php @@ -18,7 +18,7 @@ class CleanListener extends BaseListener * * @return array The array of events */ - public static function getSubscribedEvents() + public static function getSubscribedEvents(): array { return [ 'propel.pre_update' => 'onUpload', @@ -27,8 +27,10 @@ public static function getSubscribedEvents() /** * @param GenericEvent $event The event + * + * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException */ - public function onUpload(GenericEvent $event) + public function onUpload(GenericEvent $event): void { $object = $this->adapter->getObjectFromArgs($event); diff --git a/EventListener/Propel/InjectListener.php b/EventListener/Propel/InjectListener.php index ab5e228d..b25959b7 100644 --- a/EventListener/Propel/InjectListener.php +++ b/EventListener/Propel/InjectListener.php @@ -18,7 +18,7 @@ class InjectListener extends BaseListener * * @return array The array of events */ - public static function getSubscribedEvents() + public static function getSubscribedEvents(): array { return [ 'propel.post_hydrate' => 'onHydrate', @@ -27,8 +27,10 @@ public static function getSubscribedEvents() /** * @param GenericEvent $event The event + * + * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException */ - public function onHydrate(GenericEvent $event) + public function onHydrate(GenericEvent $event): void { $object = $this->adapter->getObjectFromArgs($event); diff --git a/EventListener/Propel/RemoveListener.php b/EventListener/Propel/RemoveListener.php index b9ae46ee..90d74224 100644 --- a/EventListener/Propel/RemoveListener.php +++ b/EventListener/Propel/RemoveListener.php @@ -18,7 +18,7 @@ class RemoveListener extends BaseListener * * @return array The array of events */ - public static function getSubscribedEvents() + public static function getSubscribedEvents(): array { return [ 'propel.post_delete' => 'onDelete', @@ -27,8 +27,10 @@ public static function getSubscribedEvents() /** * @param GenericEvent $event The event + * + * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException */ - public function onDelete(GenericEvent $event) + public function onDelete(GenericEvent $event): void { $object = $this->adapter->getObjectFromArgs($event); diff --git a/EventListener/Propel/UploadListener.php b/EventListener/Propel/UploadListener.php index 2c15374a..139a6878 100644 --- a/EventListener/Propel/UploadListener.php +++ b/EventListener/Propel/UploadListener.php @@ -18,7 +18,7 @@ class UploadListener extends BaseListener * * @return array The array of events */ - public static function getSubscribedEvents() + public static function getSubscribedEvents(): array { return [ 'propel.pre_insert' => 'onUpload', @@ -28,8 +28,10 @@ public static function getSubscribedEvents() /** * @param GenericEvent $event The event + * + * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException */ - public function onUpload(GenericEvent $event) + public function onUpload(GenericEvent $event): void { $object = $this->adapter->getObjectFromArgs($event); diff --git a/Exception/MappingNotFoundException.php b/Exception/MappingNotFoundException.php index da947d4f..c5108afc 100644 --- a/Exception/MappingNotFoundException.php +++ b/Exception/MappingNotFoundException.php @@ -4,7 +4,7 @@ class MappingNotFoundException extends \RuntimeException { - public static function createNotFoundForClassAndField($mapping, $class, $field) + public static function createNotFoundForClassAndField($mapping, $class, $field): self { return new self( sprintf('Mapping "%s" does not exist. The configuration for the class "%s" is probably incorrect as the mapping to use for the field "%s" could not be found.', $mapping, $class, $field) diff --git a/Form/DataTransformer/FileTransformer.php b/Form/DataTransformer/FileTransformer.php index a59e4d8e..4bd2127f 100644 --- a/Form/DataTransformer/FileTransformer.php +++ b/Form/DataTransformer/FileTransformer.php @@ -6,7 +6,7 @@ class FileTransformer implements DataTransformerInterface { - public function transform($file) + public function transform($file): array { return [ 'file' => $file, diff --git a/Form/Type/VichFileType.php b/Form/Type/VichFileType.php index 95743cb4..4db2ba38 100644 --- a/Form/Type/VichFileType.php +++ b/Form/Type/VichFileType.php @@ -54,10 +54,7 @@ public function __construct(StorageInterface $storage, UploadHandler $handler, P $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); } - /** - * {@inheritdoc} - */ - public function configureOptions(OptionsResolver $resolver) + public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'allow_delete' => true, @@ -89,10 +86,7 @@ public function configureOptions(OptionsResolver $resolver) $resolver->setNormalizer('download_uri', $downloadUriNormalizer); } - /** - * {@inheritdoc} - */ - public function buildForm(FormBuilderInterface $builder, array $options) + public function buildForm(FormBuilderInterface $builder, array $options): void { $builder->add('file', Type\FileType::class, [ 'required' => $options['required'], @@ -108,14 +102,10 @@ public function buildForm(FormBuilderInterface $builder, array $options) } } - /** - * @param FormBuilderInterface $builder - * @param array $options - */ - protected function buildDeleteField(FormBuilderInterface $builder, array $options) + protected function buildDeleteField(FormBuilderInterface $builder, array $options): void { // add delete only if there is a file - $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) { + $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options): void { $form = $event->getForm(); $object = $form->getParent()->getData(); @@ -133,7 +123,7 @@ protected function buildDeleteField(FormBuilderInterface $builder, array $option }); // delete file if needed - $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { + $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event): void { $form = $event->getForm(); $object = $form->getParent()->getData(); $delete = $form->has('delete') ? $form->get('delete')->getData() : false; @@ -146,10 +136,7 @@ protected function buildDeleteField(FormBuilderInterface $builder, array $option }); } - /** - * {@inheritdoc} - */ - public function buildView(FormView $view, FormInterface $form, array $options) + public function buildView(FormView $view, FormInterface $form, array $options): void { $object = $form->getParent()->getData(); $view->vars['object'] = $object; @@ -164,10 +151,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) } } - /** - * {@inheritdoc} - */ - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'vich_file'; } @@ -185,7 +169,7 @@ protected function resolveUriOption($uriOption, $object, FormInterface $form) return $uriOption; } - protected function resolveDownloadLabel($downloadLabel, $object, FormInterface $form) + protected function resolveDownloadLabel($downloadLabel, $object, FormInterface $form): array { if (true === $downloadLabel) { $mapping = $this->factory->fromField($object, $form->getName()); @@ -197,8 +181,8 @@ protected function resolveDownloadLabel($downloadLabel, $object, FormInterface $ $result = $downloadLabel($object); return [ - 'download_label' => isset($result['download_label']) ? $result['download_label'] : $result, - 'translation_domain' => isset($result['translation_domain']) ? $result['translation_domain'] : false, + 'download_label' => $result['download_label'] ?? $result, + 'translation_domain' => $result['translation_domain'] ?? false, ]; } diff --git a/Form/Type/VichImageType.php b/Form/Type/VichImageType.php index c84b9e76..216ddfdd 100644 --- a/Form/Type/VichImageType.php +++ b/Form/Type/VichImageType.php @@ -30,7 +30,7 @@ public function __construct(StorageInterface $storage, UploadHandler $handler, P $this->cacheManager = $cacheManager; } - public function configureOptions(OptionsResolver $resolver) + public function configureOptions(OptionsResolver $resolver): void { parent::configureOptions($resolver); @@ -42,16 +42,13 @@ public function configureOptions(OptionsResolver $resolver) $resolver->setAllowedTypes('image_uri', ['bool', 'string', 'callable']); $imageUriNormalizer = function (Options $options, $imageUri) { - return null !== $imageUri ? $imageUri : $options['download_uri']; + return $imageUri ?? $options['download_uri']; }; $resolver->setNormalizer('image_uri', $imageUriNormalizer); } - /** - * {@inheritdoc} - */ - public function buildView(FormView $view, FormInterface $form, array $options) + public function buildView(FormView $view, FormInterface $form, array $options): void { $object = $form->getParent()->getData(); $view->vars['object'] = $object; @@ -80,14 +77,11 @@ public function buildView(FormView $view, FormInterface $form, array $options) $view->vars['download_uri'] = $this->resolveUriOption($options['download_uri'], $object, $form); } // required for BC - //TODO: remove for 2.0 + // TODO: remove for 2.0 $view->vars['show_download_link'] = !empty($view->vars['download_uri']); } - /** - * {@inheritdoc} - */ - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'vich_image'; } diff --git a/Handler/AbstractHandler.php b/Handler/AbstractHandler.php index c5a7345a..3ee77bed 100644 --- a/Handler/AbstractHandler.php +++ b/Handler/AbstractHandler.php @@ -22,10 +22,6 @@ abstract class AbstractHandler */ protected $storage; - /** - * @param PropertyMappingFactory $factory The mapping factory - * @param StorageInterface $storage The storage - */ public function __construct(PropertyMappingFactory $factory, StorageInterface $storage) { $this->factory = $factory; @@ -38,8 +34,10 @@ public function __construct(PropertyMappingFactory $factory, StorageInterface $s * @param string|null $className * * @return PropertyMapping|null + * + * @throws MappingNotFoundException */ - protected function getMapping($obj, $fieldName, $className = null) + protected function getMapping($obj, string $fieldName, ?string $className = null): ?PropertyMapping { $mapping = $this->factory->fromField($obj, $fieldName, $className); diff --git a/Handler/DownloadHandler.php b/Handler/DownloadHandler.php index b8a4d771..4a03c334 100644 --- a/Handler/DownloadHandler.php +++ b/Handler/DownloadHandler.php @@ -21,8 +21,12 @@ class DownloadHandler extends AbstractHandler * @param string|true $fileName True to return original file name * * @return StreamedResponse + * + * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException + * @throws NoFileFoundException + * @throws \InvalidArgumentException */ - public function downloadObject($object, $field, $className = null, $fileName = null) + public function downloadObject($object, string $field, ?string $className = null, $fileName = null): StreamedResponse { $mapping = $this->getMapping($object, $field, $className); $stream = $this->storage->resolveStream($object, $field, $className); @@ -45,15 +49,17 @@ public function downloadObject($object, $field, $className = null, $fileName = n } /** - * @param resource $stream - * @param string $filename - * @param string $mimeType + * @param resource $stream + * @param string $filename + * @param string|null $mimeType * * @return StreamedResponse + * + * @throws \InvalidArgumentException */ - private function createDownloadResponse($stream, $filename, $mimeType = 'application/octet-stream') + private function createDownloadResponse($stream, string $filename, ?string $mimeType = 'application/octet-stream'): StreamedResponse { - $response = new StreamedResponse(function () use ($stream) { + $response = new StreamedResponse(function () use ($stream): void { stream_copy_to_stream($stream, fopen('php://output', 'wb')); }); diff --git a/Handler/UploadHandler.php b/Handler/UploadHandler.php index ed4728eb..9b51b445 100644 --- a/Handler/UploadHandler.php +++ b/Handler/UploadHandler.php @@ -28,14 +28,6 @@ class UploadHandler extends AbstractHandler */ protected $dispatcher; - /** - * Constructs a new instance of UploaderListener. - * - * @param PropertyMappingFactory $factory The mapping factory - * @param StorageInterface $storage The storage - * @param FileInjectorInterface $injector The injector - * @param EventDispatcherInterface $dispatcher The event dispatcher - */ public function __construct(PropertyMappingFactory $factory, StorageInterface $storage, FileInjectorInterface $injector, EventDispatcherInterface $dispatcher) { parent::__construct($factory, $storage); @@ -49,8 +41,10 @@ public function __construct(PropertyMappingFactory $factory, StorageInterface $s * * @param object $obj The object * @param string $fieldName The name of the field containing the upload (has to be mapped) + * + * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException */ - public function upload($obj, $fieldName) + public function upload($obj, string $fieldName): void { $mapping = $this->getMapping($obj, $fieldName); @@ -67,7 +61,7 @@ public function upload($obj, $fieldName) $this->dispatch(Events::POST_UPLOAD, new Event($obj, $mapping)); } - public function inject($obj, $fieldName) + public function inject($obj, string $fieldName): void { $mapping = $this->getMapping($obj, $fieldName); @@ -78,7 +72,7 @@ public function inject($obj, $fieldName) $this->dispatch(Events::POST_INJECT, new Event($obj, $mapping)); } - public function clean($obj, $fieldName) + public function clean($obj, string $fieldName): void { $mapping = $this->getMapping($obj, $fieldName); @@ -90,7 +84,7 @@ public function clean($obj, $fieldName) $this->remove($obj, $fieldName); } - public function remove($obj, $fieldName) + public function remove($obj, string $fieldName): void { $mapping = $this->getMapping($obj, $fieldName); $oldFilename = $mapping->getFileName($obj); @@ -108,12 +102,12 @@ public function remove($obj, $fieldName) $this->dispatch(Events::POST_REMOVE, new Event($obj, $mapping)); } - protected function dispatch($eventName, Event $event) + protected function dispatch(string $eventName, Event $event): void { $this->dispatcher->dispatch($eventName, $event); } - protected function hasUploadedFile($obj, PropertyMapping $mapping) + protected function hasUploadedFile($obj, PropertyMapping $mapping): bool { $file = $mapping->getFile($obj); diff --git a/Injector/FileInjector.php b/Injector/FileInjector.php index a7dde3f1..1ab6f94a 100644 --- a/Injector/FileInjector.php +++ b/Injector/FileInjector.php @@ -18,20 +18,12 @@ class FileInjector implements FileInjectorInterface */ protected $storage; - /** - * Constructs a new instance of FileInjector. - * - * @param StorageInterface $storage Storage - */ public function __construct(StorageInterface $storage) { $this->storage = $storage; } - /** - * {@inheritdoc} - */ - public function injectFile($obj, PropertyMapping $mapping) + public function injectFile($obj, PropertyMapping $mapping): void { $path = $this->storage->resolvePath($obj, $mapping->getFilePropertyName()); diff --git a/Injector/FileInjectorInterface.php b/Injector/FileInjectorInterface.php index 115591f5..1331d500 100644 --- a/Injector/FileInjectorInterface.php +++ b/Injector/FileInjectorInterface.php @@ -20,5 +20,5 @@ interface FileInjectorInterface * @param object $obj The object * @param PropertyMapping $mapping The mapping representing the field to populate */ - public function injectFile($obj, PropertyMapping $mapping); + public function injectFile($obj, PropertyMapping $mapping): void; } diff --git a/Mapping/PropertyMapping.php b/Mapping/PropertyMapping.php index f507efbb..eb4750f5 100644 --- a/Mapping/PropertyMapping.php +++ b/Mapping/PropertyMapping.php @@ -2,8 +2,9 @@ namespace Vich\UploaderBundle\Mapping; -use Symfony\Component\HttpFoundation\File\UploadedFile; +use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\PropertyAccess\PropertyAccess; +use Symfony\Component\PropertyAccess\PropertyAccessor; use Vich\UploaderBundle\Naming\DirectoryNamerInterface; use Vich\UploaderBundle\Naming\NamerInterface; @@ -47,16 +48,16 @@ class PropertyMapping ]; /** - * @var PropertyAccess + * @var PropertyAccessor */ protected $accessor; /** - * @param string $filePropertyPath The path to the "file" property - * @param string $fileNamePropertyPath The path to the "filename" property - * @param string[] $propertyPaths The paths to the "size", "mimeType" and "originalName" properties + * @param string $filePropertyPath The path to the "file" property + * @param string $fileNamePropertyPath The path to the "filename" property + * @param array|string[] $propertyPaths The paths to other properties */ - public function __construct($filePropertyPath, $fileNamePropertyPath, array $propertyPaths = []) + public function __construct(string $filePropertyPath, string $fileNamePropertyPath, array $propertyPaths = []) { $this->propertyPaths = array_merge( $this->propertyPaths, @@ -70,9 +71,11 @@ public function __construct($filePropertyPath, $fileNamePropertyPath, array $pro * * @param object $obj The object * - * @return UploadedFile The file + * @return File|null The file + * + * @throws \InvalidArgumentException */ - public function getFile($obj) + public function getFile($obj): ?File { return $this->readProperty($obj, 'file'); } @@ -80,10 +83,13 @@ public function getFile($obj) /** * Modifies the file property value for the given object. * - * @param object $obj The object - * @param UploadedFile $file The new file + * @param object $obj The object + * @param File $file The new file + * + * @throws \InvalidArgumentException + * @throws \TypeError */ - public function setFile($obj, $file) + public function setFile($obj, File $file): void { $this->writeProperty($obj, 'file', $file); } @@ -94,8 +100,10 @@ public function setFile($obj, $file) * @param object $obj The object * * @return string The filename + * + * @throws \InvalidArgumentException */ - public function getFileName($obj) + public function getFileName($obj): ?string { return $this->readProperty($obj, 'name'); } @@ -105,8 +113,11 @@ public function getFileName($obj) * * @param object $obj The object * @param string $value + * + * @throws \InvalidArgumentException + * @throws \TypeError */ - public function setFileName($obj, $value) + public function setFileName($obj, string $value): void { $this->writeProperty($obj, 'name', $value); } @@ -115,8 +126,11 @@ public function setFileName($obj, $value) * Removes value for each file-related property of the given object. * * @param object $obj The object + * + * @throws \InvalidArgumentException + * @throws \TypeError */ - public function erase($obj) + public function erase($obj): void { foreach (['name', 'size', 'mimeType', 'originalName', 'dimensions'] as $property) { $this->writeProperty($obj, $property, null); @@ -132,6 +146,8 @@ public function erase($obj) * @param string $property The property to read * * @return mixed + * + * @throws \InvalidArgumentException */ public function readProperty($obj, $property) { @@ -157,8 +173,11 @@ public function readProperty($obj, $property) * @param object $obj The object to which write * @param string $property The property to write * @param mixed $value The value which should be written + * + * @throws \InvalidArgumentException + * @throws \TypeError */ - public function writeProperty($obj, $property, $value) + public function writeProperty($obj, string $property, $value): void { if (!array_key_exists($property, $this->propertyPaths)) { throw new \InvalidArgumentException(sprintf('Unknown property %s', $property)); @@ -178,7 +197,7 @@ public function writeProperty($obj, $property, $value) * * @return string The name */ - public function getFilePropertyName() + public function getFilePropertyName(): string { return $this->propertyPaths['file']; } @@ -188,7 +207,7 @@ public function getFilePropertyName() * * @return string The name */ - public function getFileNamePropertyName() + public function getFileNamePropertyName(): string { return $this->propertyPaths['name']; } @@ -198,7 +217,7 @@ public function getFileNamePropertyName() * * @return NamerInterface|null The namer */ - public function getNamer() + public function getNamer(): ?NamerInterface { return $this->namer; } @@ -208,7 +227,7 @@ public function getNamer() * * @param NamerInterface $namer The namer */ - public function setNamer(NamerInterface $namer) + public function setNamer(NamerInterface $namer): void { $this->namer = $namer; } @@ -218,7 +237,7 @@ public function setNamer(NamerInterface $namer) * * @return bool True if has namer, false otherwise */ - public function hasNamer() + public function hasNamer(): bool { return null !== $this->namer; } @@ -228,7 +247,7 @@ public function hasNamer() * * @return DirectoryNamerInterface|null The directory namer */ - public function getDirectoryNamer() + public function getDirectoryNamer(): ?DirectoryNamerInterface { return $this->directoryNamer; } @@ -238,7 +257,7 @@ public function getDirectoryNamer() * * @param DirectoryNamerInterface $directoryNamer The directory namer */ - public function setDirectoryNamer(DirectoryNamerInterface $directoryNamer) + public function setDirectoryNamer(DirectoryNamerInterface $directoryNamer): void { $this->directoryNamer = $directoryNamer; } @@ -248,7 +267,7 @@ public function setDirectoryNamer(DirectoryNamerInterface $directoryNamer) * * @return bool True if has directory namer, false otherwise */ - public function hasDirectoryNamer() + public function hasDirectoryNamer(): bool { return null !== $this->directoryNamer; } @@ -258,7 +277,7 @@ public function hasDirectoryNamer() * * @param array $mapping The mapping; */ - public function setMapping(array $mapping) + public function setMapping(array $mapping): void { $this->mapping = $mapping; } @@ -268,7 +287,7 @@ public function setMapping(array $mapping) * * @return string The mapping name */ - public function getMappingName() + public function getMappingName(): string { return $this->mappingName; } @@ -278,7 +297,7 @@ public function getMappingName() * * @param string $mappingName The mapping name */ - public function setMappingName($mappingName) + public function setMappingName($mappingName): void { $this->mappingName = $mappingName; } @@ -290,7 +309,7 @@ public function setMappingName($mappingName) * * @return string The upload name */ - public function getUploadName($obj) + public function getUploadName($obj): string { if (!$this->hasNamer()) { return $this->getFile($obj)->getClientOriginalName(); @@ -304,9 +323,9 @@ public function getUploadName($obj) * * @param object $obj * - * @return string The upload directory + * @return string|null The upload directory */ - public function getUploadDir($obj) + public function getUploadDir($obj): ?string { if (!$this->hasDirectoryNamer()) { return ''; @@ -325,7 +344,7 @@ public function getUploadDir($obj) * * @return string The configured upload directory */ - public function getUploadDestination() + public function getUploadDestination(): string { return $this->mapping['upload_destination']; } @@ -335,7 +354,7 @@ public function getUploadDestination() * * @return string */ - public function getUriPrefix() + public function getUriPrefix(): string { return $this->mapping['uri_prefix']; } @@ -351,7 +370,7 @@ public function getUriPrefix() * * @return string The fixed property path */ - protected function fixPropertyPath($object, $propertyPath) + protected function fixPropertyPath($object, string $propertyPath): string { if (!is_array($object)) { return $propertyPath; @@ -360,7 +379,7 @@ protected function fixPropertyPath($object, $propertyPath) return '[' === $propertyPath[0] ? $propertyPath : sprintf('[%s]', $propertyPath); } - protected function getAccessor() + protected function getAccessor(): PropertyAccessor { //TODO: reuse original property accessor from forms if (null !== $this->accessor) { diff --git a/Mapping/PropertyMappingFactory.php b/Mapping/PropertyMappingFactory.php index c02e3dc0..d63850e6 100644 --- a/Mapping/PropertyMappingFactory.php +++ b/Mapping/PropertyMappingFactory.php @@ -37,15 +37,7 @@ class PropertyMappingFactory */ protected $defaultFilenameAttributeSuffix; - /** - * Constructs a new instance of PropertyMappingFactory. - * - * @param ContainerInterface $container The container - * @param MetadataReader $metadata The mapping mapping - * @param array $mappings The configured mappings - * @param string $defaultFilenameAttributeSuffix The default suffix to be used if the fileNamePropertyPath isn't given for a mapping - */ - public function __construct(ContainerInterface $container, MetadataReader $metadata, array $mappings, $defaultFilenameAttributeSuffix = '_name') + public function __construct(ContainerInterface $container, MetadataReader $metadata, array $mappings, ?string $defaultFilenameAttributeSuffix = '_name') { $this->container = $container; $this->metadata = $metadata; @@ -58,12 +50,17 @@ public function __construct(ContainerInterface $container, MetadataReader $metad * configuration for the uploadable fields in the specified * object. * - * @param object $obj The object - * @param string $className The object's class. Mandatory if $obj can't be used to determine it + * @param object $obj The object + * @param string $className The object's class. Mandatory if $obj can't be used to determine it + * @param string $mappingName The mapping name + * + * @return array|PropertyMapping[] An array up PropertyMapping objects * - * @return PropertyMapping[] An array up PropertyMapping objects + * @throws \RuntimeException + * @throws MappingNotFoundException + * @throws NotUploadableException */ - public function fromObject($obj, $className = null, $mappingName = null) + public function fromObject($obj, ?string $className = null, ?string $mappingName = null): array { if ($obj instanceof Proxy) { $obj->__load(); @@ -93,8 +90,12 @@ public function fromObject($obj, $className = null, $mappingName = null) * @param string $className The object's class. Mandatory if $obj can't be used to determine it * * @return PropertyMapping|null The property mapping + * + * @throws \RuntimeException + * @throws MappingNotFoundException + * @throws NotUploadableException */ - public function fromField($obj, $field, $className = null) + public function fromField($obj, string $field, ?string $className = null): ?PropertyMapping { if ($obj instanceof Proxy) { $obj->__load(); @@ -118,7 +119,7 @@ public function fromField($obj, $field, $className = null) * * @throws NotUploadableException */ - protected function checkUploadable($class) + protected function checkUploadable(string $class): void { if (!$this->metadata->isUploadable($class)) { throw new NotUploadableException(sprintf('The class "%s" is not uploadable. If you use annotations to configure VichUploaderBundle, you probably just forgot to add `@Vich\Uploadable` on top of your entity. If you don\'t use annotations, check that the configuration files are in the right place. In both cases, clearing the cache can also solve the issue.', $class)); @@ -134,16 +135,17 @@ protected function checkUploadable($class) * * @return PropertyMapping The property mapping * + * @throws \LogicException * @throws MappingNotFoundException */ - protected function createMapping($obj, $fieldName, array $mappingData) + protected function createMapping($obj, string $fieldName, array $mappingData): PropertyMapping { if (!array_key_exists($mappingData['mapping'], $this->mappings)) { throw MappingNotFoundException::createNotFoundForClassAndField($mappingData['mapping'], $this->getClassName($obj), $fieldName); } $config = $this->mappings[$mappingData['mapping']]; - $fileProperty = isset($mappingData['propertyName']) ? $mappingData['propertyName'] : $fieldName; + $fileProperty = $mappingData['propertyName'] ?? $fieldName; $fileNameProperty = empty($mappingData['fileNameProperty']) ? $fileProperty.$this->defaultFilenameAttributeSuffix : $mappingData['fileNameProperty']; $mapping = new PropertyMapping($fileProperty, $fileNameProperty, $mappingData); @@ -191,7 +193,7 @@ protected function createMapping($obj, $fieldName, array $mappingData) * * @throws \RuntimeException */ - protected function getClassName($object, $className = null) + protected function getClassName($object, ?string $className = null): string { if (null !== $className) { return $className; diff --git a/Metadata/ClassMetadata.php b/Metadata/ClassMetadata.php index a5be7865..3e480fef 100644 --- a/Metadata/ClassMetadata.php +++ b/Metadata/ClassMetadata.php @@ -8,7 +8,7 @@ class ClassMetadata extends BaseClassMetadata { public $fields = []; - public function serialize() + public function serialize(): string { return serialize([ $this->fields, @@ -16,12 +16,12 @@ public function serialize() ]); } - public function unserialize($str) + public function unserialize($str): void { - list( + [ $this->fields, $parentStr - ) = unserialize($str); + ] = unserialize($str); parent::unserialize($parentStr); } diff --git a/Metadata/MetadataReader.php b/Metadata/MetadataReader.php index 92007f83..73f2d3bd 100644 --- a/Metadata/MetadataReader.php +++ b/Metadata/MetadataReader.php @@ -36,8 +36,10 @@ public function __construct(AdvancedMetadataFactoryInterface $reader) * @param string $mapping If given, also checks that the object has the given mapping * * @return bool + * + * @throws MappingNotFoundException */ - public function isUploadable($class, $mapping = null) + public function isUploadable(string $class, ?string $mapping = null): bool { $metadata = $this->reader->getMetadataForClass($class); @@ -61,9 +63,11 @@ public function isUploadable($class, $mapping = null) /** * Search for all uploadable classes. * - * @return array A list of uploadable class names + * @return array|null A list of uploadable class names + * + * @throws \RuntimeException */ - public function getUploadableClasses() + public function getUploadableClasses(): ?array { return $this->reader->getAllClassNames(); } @@ -78,7 +82,7 @@ public function getUploadableClasses() * * @throws MappingNotFoundException */ - public function getUploadableFields(string $class, string $mapping = null): array + public function getUploadableFields(string $class, ?string $mapping = null): array { if (null === $metadata = $this->reader->getMetadataForClass($class)) { throw MappingNotFoundException::createNotFoundForClass($mapping ?? '', $class); @@ -90,7 +94,7 @@ public function getUploadableFields(string $class, string $mapping = null): arra } if (null !== $mapping) { - $uploadableFields = array_filter($uploadableFields, function ($fieldMetadata) use ($mapping) { + $uploadableFields = array_filter($uploadableFields, function (array $fieldMetadata) use ($mapping) { return $fieldMetadata['mapping'] === $mapping; }); } @@ -104,12 +108,14 @@ public function getUploadableFields(string $class, string $mapping = null): arra * @param string $class The class name to test (FQCN) * @param string $field The field * - * @return null|array The field mapping + * @return mixed The field mapping + * + * @throws MappingNotFoundException */ - public function getUploadableField($class, $field) + public function getUploadableField(string $class, string $field) { $fieldsMetadata = $this->getUploadableFields($class); - return isset($fieldsMetadata[$field]) ? $fieldsMetadata[$field] : null; + return $fieldsMetadata[$field] ?? null; } } diff --git a/Naming/Base64Namer.php b/Naming/Base64Namer.php index e9b36e44..88e04026 100644 --- a/Naming/Base64Namer.php +++ b/Naming/Base64Namer.php @@ -12,7 +12,7 @@ */ class Base64Namer implements NamerInterface, ConfigurableInterface { - const ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'; + protected const ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'; /** @var int Length of the resulting name. 10 can be decoded to a 64-bit integer. */ protected $length = 10; @@ -23,17 +23,14 @@ class Base64Namer implements NamerInterface, ConfigurableInterface * @param array $options Options for this namer. The following options are accepted: * - length: the length of the resulting name. */ - public function configure(array $options) + public function configure(array $options): void { if (isset($options['length'])) { $this->length = $options['length']; } } - /** - * {@inheritdoc} - */ - public function name($object, PropertyMapping $mapping) + public function name($object, PropertyMapping $mapping): string { $file = $mapping->getFile($object); @@ -49,7 +46,7 @@ public function name($object, PropertyMapping $mapping) return $name; } - protected function getRandomChar() + protected function getRandomChar(): string { return self::ALPHABET[random_int(0, 63)]; } diff --git a/Naming/DirectoryNamerInterface.php b/Naming/DirectoryNamerInterface.php index d49b3f17..a93f6a34 100644 --- a/Naming/DirectoryNamerInterface.php +++ b/Naming/DirectoryNamerInterface.php @@ -19,5 +19,5 @@ interface DirectoryNamerInterface * * @return string The directory name */ - public function directoryName($object, PropertyMapping $mapping); + public function directoryName($object, PropertyMapping $mapping): string; } diff --git a/Naming/HashNamer.php b/Naming/HashNamer.php index 4e0e4952..49f6e58c 100644 --- a/Naming/HashNamer.php +++ b/Naming/HashNamer.php @@ -20,7 +20,7 @@ class HashNamer implements NamerInterface, ConfigurableInterface * - algorithm: wich hash algorithm to use. * - length: limit file name length */ - public function configure(array $options) + public function configure(array $options): void { $options = array_merge(['algorithm' => $this->algorithm, 'length' => $this->length], $options); @@ -28,10 +28,7 @@ public function configure(array $options) $this->length = $options['length']; } - /** - * {@inheritdoc} - */ - public function name($object, PropertyMapping $mapping) + public function name($object, PropertyMapping $mapping): string { $file = $mapping->getFile($object); @@ -47,8 +44,8 @@ public function name($object, PropertyMapping $mapping) return $name; } - protected function getRandomString() + protected function getRandomString(): string { - return microtime(true).mt_rand(0, 9999999); + return microtime(true).random_int(0, 9999999); } } diff --git a/Naming/NamerInterface.php b/Naming/NamerInterface.php index 92006f22..b5443121 100644 --- a/Naming/NamerInterface.php +++ b/Naming/NamerInterface.php @@ -19,5 +19,5 @@ interface NamerInterface * * @return string The file name */ - public function name($object, PropertyMapping $mapping); + public function name($object, PropertyMapping $mapping): string; } diff --git a/Naming/OrignameNamer.php b/Naming/OrignameNamer.php index bdb61cb9..1f53d041 100644 --- a/Naming/OrignameNamer.php +++ b/Naming/OrignameNamer.php @@ -22,7 +22,7 @@ class OrignameNamer implements NamerInterface, ConfigurableInterface * @param array $options Options for this namer. The following options are accepted: * - transliterate: whether the filename should be transliterated or not */ - public function configure(array $options) + public function configure(array $options): void { $this->transliterate = isset($options['transliterate']) ? (bool) $options['transliterate'] : $this->transliterate; } @@ -30,8 +30,9 @@ public function configure(array $options) /** * {@inheritdoc} */ - public function name($object, PropertyMapping $mapping) + public function name($object, PropertyMapping $mapping): string { + /* @var $file UploadedFile */ $file = $mapping->getFile($object); $name = $file->getClientOriginalName(); @@ -39,8 +40,6 @@ public function name($object, PropertyMapping $mapping) $name = Transliterator::transliterate($name); } - /* @var $file UploadedFile */ - return uniqid().'_'.$name; } } diff --git a/Naming/Polyfill/FileExtensionTrait.php b/Naming/Polyfill/FileExtensionTrait.php index 80ba501c..5ad607c6 100644 --- a/Naming/Polyfill/FileExtensionTrait.php +++ b/Naming/Polyfill/FileExtensionTrait.php @@ -13,7 +13,7 @@ trait FileExtensionTrait * * @return string|null */ - private function getExtension(UploadedFile $file) + private function getExtension(UploadedFile $file): ?string { $originalName = $file->getClientOriginalName(); @@ -25,6 +25,6 @@ private function getExtension(UploadedFile $file) return $extension; } - return; + return null; } } diff --git a/Naming/PropertyDirectoryNamer.php b/Naming/PropertyDirectoryNamer.php index 6904b31e..bcaa80b8 100644 --- a/Naming/PropertyDirectoryNamer.php +++ b/Naming/PropertyDirectoryNamer.php @@ -31,9 +31,6 @@ class PropertyDirectoryNamer implements DirectoryNamerInterface, ConfigurableInt */ protected $propertyAccessor; - /** - * @param PropertyAccessorInterface $propertyAccessor Property accessor interface - */ public function __construct(PropertyAccessorInterface $propertyAccessor = null) { $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); @@ -43,8 +40,10 @@ public function __construct(PropertyAccessorInterface $propertyAccessor = null) * @param array $options Options for this namer. The following options are accepted: * - property: path to the property used to name the file. Can be either an attribute or a method. * - transliterate: whether the filename should be transliterated or not + * + * @throws \InvalidArgumentException */ - public function configure(array $options) + public function configure(array $options): void { if (empty($options['property'])) { throw new \InvalidArgumentException('Option "property" is missing or empty.'); @@ -54,10 +53,7 @@ public function configure(array $options) $this->transliterate = isset($options['transliterate']) ? (bool) $options['transliterate'] : $this->transliterate; } - /** - * {@inheritdoc} - */ - public function directoryName($object, PropertyMapping $mapping) + public function directoryName($object, PropertyMapping $mapping): string { if (empty($this->propertyPath)) { throw new \LogicException('The property to use can not be determined. Did you call the configure() method?'); diff --git a/Naming/PropertyNamer.php b/Naming/PropertyNamer.php index 5eeec9ed..ce6c4333 100644 --- a/Naming/PropertyNamer.php +++ b/Naming/PropertyNamer.php @@ -31,8 +31,10 @@ class PropertyNamer implements NamerInterface, ConfigurableInterface * @param array $options Options for this namer. The following options are accepted: * - property: path to the property used to name the file. Can be either an attribute or a method. * - transliterate: whether the filename should be transliterated or not + * + * @throws \InvalidArgumentException */ - public function configure(array $options) + public function configure(array $options): void { if (empty($options['property'])) { throw new \InvalidArgumentException('Option "property" is missing or empty.'); @@ -42,10 +44,7 @@ public function configure(array $options) $this->transliterate = isset($options['transliterate']) ? (bool) $options['transliterate'] : $this->transliterate; } - /** - * {@inheritdoc} - */ - public function name($object, PropertyMapping $mapping) + public function name($object, PropertyMapping $mapping): string { if (empty($this->propertyPath)) { throw new \LogicException('The property to use can not be determined. Did you call the configure() method?'); diff --git a/Naming/SubdirDirectoryNamer.php b/Naming/SubdirDirectoryNamer.php index 8c738ffa..30d732ef 100644 --- a/Naming/SubdirDirectoryNamer.php +++ b/Naming/SubdirDirectoryNamer.php @@ -20,7 +20,7 @@ class SubdirDirectoryNamer implements DirectoryNamerInterface, ConfigurableInter * - chars_per_dir: how many chars use for each dir. * - dirs: how many dirs create */ - public function configure(array $options) + public function configure(array $options): void { $options = array_merge(['chars_per_dir' => $this->charsPerDir, 'dirs' => $this->dirs], $options); @@ -28,10 +28,7 @@ public function configure(array $options) $this->dirs = $options['dirs']; } - /** - * {@inheritdoc} - */ - public function directoryName($object, PropertyMapping $mapping) + public function directoryName($object, PropertyMapping $mapping): string { $fileName = $mapping->getFileName($object); diff --git a/Naming/UniqidNamer.php b/Naming/UniqidNamer.php index 54bd33c9..2507cc1a 100644 --- a/Naming/UniqidNamer.php +++ b/Naming/UniqidNamer.php @@ -13,10 +13,7 @@ class UniqidNamer implements NamerInterface { use Polyfill\FileExtensionTrait; - /** - * {@inheritdoc} - */ - public function name($object, PropertyMapping $mapping) + public function name($object, PropertyMapping $mapping): string { $file = $mapping->getFile($object); $name = str_replace('.', '', uniqid('', true)); diff --git a/Storage/AbstractStorage.php b/Storage/AbstractStorage.php index 8476342c..509b818b 100644 --- a/Storage/AbstractStorage.php +++ b/Storage/AbstractStorage.php @@ -2,10 +2,10 @@ namespace Vich\UploaderBundle\Storage; +use Symfony\Component\HttpFoundation\File\UploadedFile; use Vich\UploaderBundle\Exception\MappingNotFoundException; -use Vich\UploaderBundle\Mapping\PropertyMappingFactory; use Vich\UploaderBundle\Mapping\PropertyMapping; -use Symfony\Component\HttpFoundation\File\UploadedFile; +use Vich\UploaderBundle\Mapping\PropertyMappingFactory; /** * FileSystemStorage. @@ -19,32 +19,14 @@ abstract class AbstractStorage implements StorageInterface */ protected $factory; - /** - * Constructs a new instance of FileSystemStorage. - * - * @param PropertyMappingFactory $factory The factory - */ public function __construct(PropertyMappingFactory $factory) { $this->factory = $factory; } - /** - * Do real upload. - * - * @param PropertyMapping $mapping - * @param UploadedFile $file - * @param string $dir - * @param string $name - * - * @return bool - */ - abstract protected function doUpload(PropertyMapping $mapping, UploadedFile $file, $dir, $name); + abstract protected function doUpload(PropertyMapping $mapping, UploadedFile $file, ?string $dir, string $name); - /** - * {@inheritdoc} - */ - public function upload($obj, PropertyMapping $mapping) + public function upload($obj, PropertyMapping $mapping): void { $file = $mapping->getFile($obj); @@ -59,7 +41,7 @@ public function upload($obj, PropertyMapping $mapping) $mapping->writeProperty($obj, 'mimeType', $file->getMimeType()); $mapping->writeProperty($obj, 'originalName', $file->getClientOriginalName()); - if (strpos($file->getMimeType(), 'image/') !== false) { + if (false !== strpos($file->getMimeType(), 'image/')) { $dimensions = getimagesize($file); $mapping->writeProperty($obj, 'dimensions', array_splice($dimensions, 0, 2)); } @@ -69,21 +51,9 @@ public function upload($obj, PropertyMapping $mapping) $this->doUpload($mapping, $file, $dir, $name); } - /** - * Do real remove. - * - * @param PropertyMapping $mapping - * @param string $dir - * @param string $name - * - * @return bool - */ - abstract protected function doRemove(PropertyMapping $mapping, $dir, $name); + abstract protected function doRemove(PropertyMapping $mapping, ?string $dir, string $name): ?bool; - /** - * {@inheritdoc} - */ - public function remove($obj, PropertyMapping $mapping) + public function remove($obj, PropertyMapping $mapping): ?bool { $name = $mapping->getFileName($obj); @@ -104,31 +74,25 @@ public function remove($obj, PropertyMapping $mapping) * * @return string */ - abstract protected function doResolvePath(PropertyMapping $mapping, $dir, $name, $relative = false); + abstract protected function doResolvePath(PropertyMapping $mapping, ?string $dir, string $name, ?bool $relative = false): string; - /** - * {@inheritdoc} - */ - public function resolvePath($obj, $fieldName, $className = null, $relative = false) + public function resolvePath($obj, string $fieldName, ?string $className = null, ?bool $relative = false): ?string { - list($mapping, $filename) = $this->getFilename($obj, $fieldName, $className); + [$mapping, $filename] = $this->getFilename($obj, $fieldName, $className); if (empty($filename)) { - return; + return null; } return $this->doResolvePath($mapping, $mapping->getUploadDir($obj), $filename, $relative); } - /** - * {@inheritdoc} - */ - public function resolveUri($obj, $fieldName, $className = null) + public function resolveUri($obj, string $fieldName, ?string $className = null): ?string { - list($mapping, $filename) = $this->getFilename($obj, $fieldName, $className); + [$mapping, $filename] = $this->getFilename($obj, $fieldName, $className); if (empty($filename)) { - return; + return null; } $dir = $mapping->getUploadDir($obj); @@ -137,24 +101,31 @@ public function resolveUri($obj, $fieldName, $className = null) return $mapping->getUriPrefix().'/'.$path; } - /** - * {@inheritdoc} - */ - public function resolveStream($obj, $fieldName, $className = null) + public function resolveStream($obj, string $fieldName, ?string $className = null) { $path = $this->resolvePath($obj, $fieldName, $className); if (empty($path)) { - return; + return null; } return fopen($path, 'rb'); } /** - * note: extension point. + * note: extension point. + * + * @param $obj + * @param string $fieldName + * @param null|string $className + * + * @return array + * + * @throws MappingNotFoundException + * @throws \RuntimeException + * @throws \Vich\UploaderBundle\Exception\NotUploadableException */ - protected function getFilename($obj, $fieldName, $className = null) + protected function getFilename($obj, string $fieldName, ?string $className = null): array { $mapping = $this->factory->fromField($obj, $fieldName, $className); diff --git a/Storage/FileSystemStorage.php b/Storage/FileSystemStorage.php index a316722b..18729af3 100644 --- a/Storage/FileSystemStorage.php +++ b/Storage/FileSystemStorage.php @@ -12,30 +12,21 @@ */ class FileSystemStorage extends AbstractStorage { - /** - * {@inheritdoc} - */ - protected function doUpload(PropertyMapping $mapping, UploadedFile $file, $dir, $name) + protected function doUpload(PropertyMapping $mapping, UploadedFile $file, ?string $dir, string $name) { $uploadDir = $mapping->getUploadDestination().DIRECTORY_SEPARATOR.$dir; return $file->move($uploadDir, $name); } - /** - * {@inheritdoc} - */ - protected function doRemove(PropertyMapping $mapping, $dir, $name) + protected function doRemove(PropertyMapping $mapping, ?string $dir, string $name): ?bool { $file = $this->doResolvePath($mapping, $dir, $name); return file_exists($file) ? unlink($file) : false; } - /** - * {@inheritdoc} - */ - protected function doResolvePath(PropertyMapping $mapping, $dir, $name, $relative = false) + protected function doResolvePath(PropertyMapping $mapping, ?string $dir, string $name, ?bool $relative = false): string { $path = !empty($dir) ? $dir.DIRECTORY_SEPARATOR.$name : $name; @@ -46,15 +37,12 @@ protected function doResolvePath(PropertyMapping $mapping, $dir, $name, $relativ return $mapping->getUploadDestination().DIRECTORY_SEPARATOR.$path; } - /** - * {@inheritdoc} - */ - public function resolveUri($obj, $mappingName, $className = null) + public function resolveUri($obj, string $mappingName, string $className = null): ?string { - list($mapping, $name) = $this->getFilename($obj, $mappingName, $className); + [$mapping, $name] = $this->getFilename($obj, $mappingName, $className); if (empty($name)) { - return; + return null; } $uploadDir = $this->convertWindowsDirectorySeparator($mapping->getUploadDir($obj)); @@ -63,7 +51,7 @@ public function resolveUri($obj, $mappingName, $className = null) return sprintf('%s/%s', $mapping->getUriPrefix(), $uploadDir.$name); } - private function convertWindowsDirectorySeparator($string) + private function convertWindowsDirectorySeparator(string $string): string { return str_replace('\\', '/', $string); } diff --git a/Storage/FlysystemStorage.php b/Storage/FlysystemStorage.php index a22e0ae1..41f0fa67 100644 --- a/Storage/FlysystemStorage.php +++ b/Storage/FlysystemStorage.php @@ -2,8 +2,8 @@ namespace Vich\UploaderBundle\Storage; -use League\Flysystem\FilesystemInterface; use League\Flysystem\FileNotFoundException; +use League\Flysystem\FilesystemInterface; use League\Flysystem\MountManager; use Symfony\Component\HttpFoundation\File\UploadedFile; use Vich\UploaderBundle\Mapping\PropertyMapping; @@ -15,16 +15,10 @@ class FlysystemStorage extends AbstractStorage { /** - * @var FilesystemMap + * @var FilesystemMap Gaufrette filesystem factory */ protected $mountManager; - /** - * Constructs a new instance of FlysystemStorage. - * - * @param PropertyMappingFactory $factory The factory - * @param MountManager $mountManager Gaufrete filesystem factory - */ public function __construct(PropertyMappingFactory $factory, MountManager $mountManager) { parent::__construct($factory); @@ -35,7 +29,7 @@ public function __construct(PropertyMappingFactory $factory, MountManager $mount /** * {@inheritdoc} */ - protected function doUpload(PropertyMapping $mapping, UploadedFile $file, $dir, $name) + protected function doUpload(PropertyMapping $mapping, UploadedFile $file, ?string $dir, string $name): void { $fs = $this->getFilesystem($mapping); $path = !empty($dir) ? $dir.'/'.$name : $name; @@ -46,10 +40,7 @@ protected function doUpload(PropertyMapping $mapping, UploadedFile $file, $dir, ]); } - /** - * {@inheritdoc} - */ - protected function doRemove(PropertyMapping $mapping, $dir, $name) + protected function doRemove(PropertyMapping $mapping, ?string $dir, string $name): ?bool { $fs = $this->getFilesystem($mapping); $path = !empty($dir) ? $dir.'/'.$name : $name; @@ -61,10 +52,7 @@ protected function doRemove(PropertyMapping $mapping, $dir, $name) } } - /** - * {@inheritdoc} - */ - protected function doResolvePath(PropertyMapping $mapping, $dir, $name, $relative = false) + protected function doResolvePath(PropertyMapping $mapping, ?string $dir, string $name, ?bool $relative = false): string { $fs = $this->getFilesystem($mapping); $path = !empty($dir) ? $dir.'/'.$name : $name; @@ -73,34 +61,24 @@ protected function doResolvePath(PropertyMapping $mapping, $dir, $name, $relativ return $path; } - return $fs->get($path)->getPath(); + return (string) $fs->get($path)->getPath(); } - /** - * {@inheritdoc} - */ - public function resolveStream($obj, $fieldName, $className = null) + public function resolveStream($obj, string $fieldName, string $className = null): ?string { $path = $this->resolvePath($obj, $fieldName, $className); if (empty($path)) { - return; + return null; } $mapping = $this->factory->fromField($obj, $fieldName, $className); $fs = $this->getFilesystem($mapping); - return $fs->readStream($path); + return (string) $fs->readStream($path); } - /** - * Get filesystem adapter by key. - * - * @param string $key - * - * @return FilesystemInterface - */ - protected function getFilesystem(PropertyMapping $mapping) + protected function getFilesystem(PropertyMapping $mapping): FilesystemInterface { return $this->mountManager->getFilesystem($mapping->getUploadDestination()); } diff --git a/Storage/GaufretteStorage.php b/Storage/GaufretteStorage.php index 7d907033..3b21caf3 100644 --- a/Storage/GaufretteStorage.php +++ b/Storage/GaufretteStorage.php @@ -2,9 +2,9 @@ namespace Vich\UploaderBundle\Storage; -use Gaufrette\Filesystem; use Gaufrette\Adapter\MetadataSupporter; use Gaufrette\Exception\FileNotFound; +use Gaufrette\Filesystem; use Knp\Bundle\GaufretteBundle\FilesystemMap; use Symfony\Component\HttpFoundation\File\UploadedFile; use Vich\UploaderBundle\Mapping\PropertyMapping; @@ -34,7 +34,7 @@ class GaufretteStorage extends AbstractStorage * @param FilesystemMap $filesystemMap Gaufrete filesystem factory * @param string $protocol Gaufrette stream wrapper protocol */ - public function __construct(PropertyMappingFactory $factory, FilesystemMap $filesystemMap, $protocol = 'gaufrette') + public function __construct(PropertyMappingFactory $factory, FilesystemMap $filesystemMap, string $protocol = 'gaufrette') { parent::__construct($factory); @@ -42,10 +42,7 @@ public function __construct(PropertyMappingFactory $factory, FilesystemMap $file $this->protocol = $protocol; } - /** - * {@inheritdoc} - */ - protected function doUpload(PropertyMapping $mapping, UploadedFile $file, $dir, $name) + protected function doUpload(PropertyMapping $mapping, UploadedFile $file, ?string $dir, string $name): void { $filesystem = $this->getFilesystem($mapping); $path = !empty($dir) ? $dir.'/'.$name : $name; @@ -57,10 +54,7 @@ protected function doUpload(PropertyMapping $mapping, UploadedFile $file, $dir, $filesystem->write($path, file_get_contents($file->getPathname()), true); } - /** - * {@inheritdoc} - */ - protected function doRemove(PropertyMapping $mapping, $dir, $name) + protected function doRemove(PropertyMapping $mapping, ?string $dir, string $name): ?bool { $filesystem = $this->getFilesystem($mapping); $path = !empty($dir) ? $dir.'/'.$name : $name; @@ -72,10 +66,7 @@ protected function doRemove(PropertyMapping $mapping, $dir, $name) } } - /** - * {@inheritdoc} - */ - protected function doResolvePath(PropertyMapping $mapping, $dir, $name, $relative = false) + protected function doResolvePath(PropertyMapping $mapping, ?string $dir, string $name, ?bool $relative = false): string { $path = !empty($dir) ? $dir.'/'.$name : $name; @@ -93,7 +84,7 @@ protected function doResolvePath(PropertyMapping $mapping, $dir, $name, $relativ * * @return Filesystem */ - protected function getFilesystem(PropertyMapping $mapping) + protected function getFilesystem(PropertyMapping $mapping): Filesystem { return $this->filesystemMap->get($mapping->getUploadDestination()); } diff --git a/Storage/StorageInterface.php b/Storage/StorageInterface.php index 0ee40657..e10ad529 100644 --- a/Storage/StorageInterface.php +++ b/Storage/StorageInterface.php @@ -16,7 +16,7 @@ interface StorageInterface * @param object $obj The object * @param PropertyMapping $mapping The mapping representing the field to upload */ - public function upload($obj, PropertyMapping $mapping); + public function upload($obj, PropertyMapping $mapping): void; /** * Removes the files associated with the given mapping. @@ -24,7 +24,7 @@ public function upload($obj, PropertyMapping $mapping); * @param object $obj The object * @param PropertyMapping $mapping The mapping representing the field to remove */ - public function remove($obj, PropertyMapping $mapping); + public function remove($obj, PropertyMapping $mapping): ?bool; /** * Resolves the path for a file based on the specified object @@ -37,7 +37,7 @@ public function remove($obj, PropertyMapping $mapping); * * @return string The path */ - public function resolvePath($obj, $fieldName, $className = null, $relative = false); + public function resolvePath($obj, string $fieldName, ?string $className = null, ?bool $relative = false): ?string; //TODO: inconsistency - use PropertyMapping instead of fieldName+className @@ -50,11 +50,10 @@ public function resolvePath($obj, $fieldName, $className = null, $relative = fal * * @return string|null The uri or null if file not stored */ - public function resolveUri($obj, $fieldName, $className = null); + public function resolveUri($obj, string $fieldName, ?string $className = null): ?string; /** - * Returns a read-only stream based on the specified object and mapping - * name. + * Returns a read-only stream based on the specified object and mapping name. * * @param object|array $obj The object * @param string $fieldName The field to use @@ -62,5 +61,5 @@ public function resolveUri($obj, $fieldName, $className = null); * * @return resource|null The resolved resource or null if file not stored */ - public function resolveStream($obj, $fieldName, $className = null); + public function resolveStream($obj, string $fieldName, ?string $className = null); } diff --git a/Templating/Helper/UploaderHelper.php b/Templating/Helper/UploaderHelper.php index a128d4f9..6b5faa49 100644 --- a/Templating/Helper/UploaderHelper.php +++ b/Templating/Helper/UploaderHelper.php @@ -17,22 +17,12 @@ class UploaderHelper extends Helper */ protected $storage; - /** - * Constructs a new instance of UploaderHelper. - * - * @param StorageInterface $storage The storage - */ public function __construct(StorageInterface $storage) { $this->storage = $storage; } - /** - * Gets the helper name. - * - * @return string The name - */ - public function getName() + public function getName(): string { return 'vich_uploader'; } @@ -47,7 +37,7 @@ public function getName() * * @return string|null The public asset path or null if file not stored */ - public function asset($obj, $fieldName, $className = null) + public function asset($obj, string $fieldName, ?string $className = null) { return $this->storage->resolveUri($obj, $fieldName, $className); } diff --git a/Tests/Adapter/ODM/MongoDB/MongoDBAdapterTest.php b/Tests/Adapter/ODM/MongoDB/MongoDBAdapterTest.php index 0925c863..2f44e9a5 100644 --- a/Tests/Adapter/ODM/MongoDB/MongoDBAdapterTest.php +++ b/Tests/Adapter/ODM/MongoDB/MongoDBAdapterTest.php @@ -13,7 +13,7 @@ */ class MongoDBAdapterTest extends TestCase { - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { if (!class_exists('Doctrine\ODM\MongoDB\Event\LifecycleEventArgs')) { self::markTestSkipped('Doctrine\ODM\MongoDB\Event\LifecycleEventArgs does not exist.'); @@ -23,7 +23,7 @@ public static function setUpBeforeClass() /** * Test the getObjectFromArgs method. */ - public function testGetObjectFromArgs() + public function testGetObjectFromArgs(): void { $entity = new DummyEntity(); diff --git a/Tests/Adapter/ORM/DoctrineORMAdapterTest.php b/Tests/Adapter/ORM/DoctrineORMAdapterTest.php index 4c7fafe1..e44b0bd4 100644 --- a/Tests/Adapter/ORM/DoctrineORMAdapterTest.php +++ b/Tests/Adapter/ORM/DoctrineORMAdapterTest.php @@ -13,7 +13,7 @@ */ class DoctrineORMAdapterTest extends TestCase { - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { if (!class_exists('Doctrine\ORM\Event\LifecycleEventArgs')) { self::markTestSkipped('Doctrine\ORM\Event\LifecycleEventArgs does not exist.'); @@ -23,7 +23,7 @@ public static function setUpBeforeClass() /** * Test the getObjectFromArgs method. */ - public function testGetObjectFromArgs() + public function testGetObjectFromArgs(): void { $entity = new DummyEntity(); diff --git a/Tests/Adapter/PHPCR/PHPCRAdapterTest.php b/Tests/Adapter/PHPCR/PHPCRAdapterTest.php index 3f51dd17..7f5161d2 100644 --- a/Tests/Adapter/PHPCR/PHPCRAdapterTest.php +++ b/Tests/Adapter/PHPCR/PHPCRAdapterTest.php @@ -12,7 +12,7 @@ */ class PHPCRAdapterTest extends TestCase { - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { if (!class_exists(LifecycleEventArgs::class)) { self::markTestSkipped('Doctrine\Common\Persistence\Event\LifecycleEventArgs does not exist.'); @@ -22,7 +22,7 @@ public static function setUpBeforeClass() /** * Test the getObjectFromArgs method. */ - public function testGetObjectFromArgs() + public function testGetObjectFromArgs(): void { $entity = new DummyEntity(); diff --git a/Tests/Adapter/Propel/PropelORMAdapterTest.php b/Tests/Adapter/Propel/PropelORMAdapterTest.php index 04001150..68231396 100644 --- a/Tests/Adapter/Propel/PropelORMAdapterTest.php +++ b/Tests/Adapter/Propel/PropelORMAdapterTest.php @@ -14,19 +14,19 @@ class PropelORMAdapterTest extends TestCase { protected $adapter; - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { if (!class_exists('Symfony\Component\EventDispatcher\GenericEvent')) { self::markTestSkipped('Symfony\Component\EventDispatcher\GenericEvent does not exist.'); } } - protected function setUp() + protected function setUp(): void { $this->adapter = new PropelORMAdapter(); } - public function testGetObjectFromArgs() + public function testGetObjectFromArgs(): void { $event = $this->createMock('\Symfony\Component\EventDispatcher\GenericEvent'); $event @@ -37,7 +37,7 @@ public function testGetObjectFromArgs() $this->assertSame(42, $this->adapter->getObjectFromArgs($event)); } - public function testRecomputeChangeset() + public function testRecomputeChangeset(): void { $event = $this->createMock('\Symfony\Component\EventDispatcher\GenericEvent'); diff --git a/Tests/DependencyInjection/VichUploaderExtensionTest.php b/Tests/DependencyInjection/VichUploaderExtensionTest.php index 1a647223..bca414cb 100644 --- a/Tests/DependencyInjection/VichUploaderExtensionTest.php +++ b/Tests/DependencyInjection/VichUploaderExtensionTest.php @@ -22,7 +22,7 @@ protected function getMinimalConfiguration() ]; } - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -33,7 +33,7 @@ protected function setUp() $this->container->setParameter('kernel.cache_dir', sys_get_temp_dir()); } - public function testStorageServiceParameterIsSet() + public function testStorageServiceParameterIsSet(): void { $this->load([ 'storage' => 'gaufrette', @@ -42,7 +42,7 @@ public function testStorageServiceParameterIsSet() $this->assertContainerBuilderHasAlias('vich_uploader.storage', 'vich_uploader.storage.gaufrette'); } - public function testStorageServiceCustom() + public function testStorageServiceCustom(): void { $this->load([ 'storage' => '@acme.storage', @@ -51,7 +51,7 @@ public function testStorageServiceCustom() $this->assertContainerBuilderHasAlias('vich_uploader.storage', 'acme.storage'); } - public function testExtraServiceFilesAreLoaded() + public function testExtraServiceFilesAreLoaded(): void { $this->load([ 'twig' => true, @@ -62,7 +62,7 @@ public function testExtraServiceFilesAreLoaded() $this->assertContainerBuilderHasService('vich_uploader.twig.extension.uploader', 'Vich\UploaderBundle\Twig\Extension\UploaderExtension'); } - public function testMappingsServiceParameterIsSet() + public function testMappingsServiceParameterIsSet(): void { $this->load([ 'mappings' => $mappings = [ @@ -84,7 +84,7 @@ public function testMappingsServiceParameterIsSet() $this->assertContainerBuilderHasParameter('vich_uploader.mappings', $mappings); } - public function testDbDriverIsNotOverridden() + public function testDbDriverIsNotOverridden(): void { $this->load([ 'db_driver' => 'propel', @@ -105,7 +105,7 @@ public function testDbDriverIsNotOverridden() $this->assertContainerBuilderHasParameter('vich_uploader.mappings', $mappings); } - public function testListenersCreation() + public function testListenersCreation(): void { $this->load([ 'db_driver' => 'mongodb', @@ -127,7 +127,7 @@ public function testListenersCreation() $this->assertContainerBuilderHasService('vich_uploader.listener.remove.profile_common_avatar'); } - public function testFormThemeCorrectlyOverridden() + public function testFormThemeCorrectlyOverridden(): void { $vichUploaderExtension = new VichUploaderExtension(); $this->container->registerExtension($vichUploaderExtension); diff --git a/Tests/DummyEntity.php b/Tests/DummyEntity.php index f1bb5718..969a203c 100644 --- a/Tests/DummyEntity.php +++ b/Tests/DummyEntity.php @@ -27,7 +27,7 @@ public function getFile() return $this->file; } - public function setFile($file) + public function setFile($file): void { $this->file = $file; } @@ -37,7 +37,7 @@ public function getFileName() return $this->fileName; } - public function setFileName($fileName) + public function setFileName($fileName): void { $this->fileName = $fileName; } @@ -47,7 +47,7 @@ public function getSize() return $this->size; } - public function setSize($size) + public function setSize($size): void { $this->size = $size; } diff --git a/Tests/EventListener/Doctrine/CleanListenerTest.php b/Tests/EventListener/Doctrine/CleanListenerTest.php index 9737742f..3ad9889a 100644 --- a/Tests/EventListener/Doctrine/CleanListenerTest.php +++ b/Tests/EventListener/Doctrine/CleanListenerTest.php @@ -14,7 +14,7 @@ class CleanListenerTest extends ListenerTestCase /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -24,7 +24,7 @@ protected function setUp() /** * Test the getSubscribedEvents method. */ - public function testGetSubscribedEvents() + public function testGetSubscribedEvents(): void { $events = $this->listener->getSubscribedEvents(); @@ -34,7 +34,7 @@ public function testGetSubscribedEvents() /** * Test the preUpdate method. */ - public function testPreUpdate() + public function testPreUpdate(): void { $this->metadata ->expects($this->once()) @@ -66,7 +66,7 @@ public function testPreUpdate() /** * Test that preUpdate skips non uploadable entity. */ - public function testPreUpdateSkipsNonUploadable() + public function testPreUpdateSkipsNonUploadable(): void { $this->metadata ->expects($this->once()) diff --git a/Tests/EventListener/Doctrine/InjectListenerTest.php b/Tests/EventListener/Doctrine/InjectListenerTest.php index a1bec907..7753f4be 100644 --- a/Tests/EventListener/Doctrine/InjectListenerTest.php +++ b/Tests/EventListener/Doctrine/InjectListenerTest.php @@ -14,7 +14,7 @@ class InjectListenerTest extends ListenerTestCase /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -24,7 +24,7 @@ protected function setUp() /** * Test the getSubscribedEvents method. */ - public function testGetSubscribedEvents() + public function testGetSubscribedEvents(): void { $events = $this->listener->getSubscribedEvents(); @@ -34,7 +34,7 @@ public function testGetSubscribedEvents() /** * Test the postLoad method. */ - public function testPostLoad() + public function testPostLoad(): void { $this->metadata ->expects($this->once()) @@ -61,7 +61,7 @@ public function testPostLoad() /** * Test that postLoad skips non uploadable entity. */ - public function testPostLoadSkipsNonUploadable() + public function testPostLoadSkipsNonUploadable(): void { $this->metadata ->expects($this->once()) diff --git a/Tests/EventListener/Doctrine/ListenerTestCase.php b/Tests/EventListener/Doctrine/ListenerTestCase.php index 7b01398d..5308a78b 100644 --- a/Tests/EventListener/Doctrine/ListenerTestCase.php +++ b/Tests/EventListener/Doctrine/ListenerTestCase.php @@ -48,7 +48,7 @@ class ListenerTestCase extends TestCase /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { $this->adapter = $this->getAdapterMock(); $this->metadata = $this->getMetadataReaderMock(); diff --git a/Tests/EventListener/Doctrine/RemoveListenerTest.php b/Tests/EventListener/Doctrine/RemoveListenerTest.php index ad0907d5..2c0d10f3 100644 --- a/Tests/EventListener/Doctrine/RemoveListenerTest.php +++ b/Tests/EventListener/Doctrine/RemoveListenerTest.php @@ -15,7 +15,7 @@ class RemoveListenerTest extends ListenerTestCase /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -25,14 +25,14 @@ protected function setUp() /** * Test the getSubscribedEvents method. */ - public function testGetSubscribedEvents() + public function testGetSubscribedEvents(): void { $events = $this->listener->getSubscribedEvents(); $this->assertSame(['preRemove', 'postRemove'], $events); } - public function testPreRemove() + public function testPreRemove(): void { $this->object = $this->getEntityProxyMock(); $this->object @@ -48,7 +48,7 @@ public function testPreRemove() $this->listener->preRemove($this->event); } - public function testPreRemoveSkipNonUploadable() + public function testPreRemoveSkipNonUploadable(): void { $this->object = $this->getEntityProxyMock(); $this->object @@ -67,7 +67,7 @@ public function testPreRemoveSkipNonUploadable() /** * Test the postRemove method. */ - public function testPostRemove() + public function testPostRemove(): void { $this->metadata ->expects($this->once()) @@ -94,7 +94,7 @@ public function testPostRemove() /** * Test that postRemove skips non uploadable entity. */ - public function testPostRemoveSkipsNonUploadable() + public function testPostRemoveSkipsNonUploadable(): void { $this->metadata ->expects($this->once()) diff --git a/Tests/EventListener/Doctrine/UploadListenerTest.php b/Tests/EventListener/Doctrine/UploadListenerTest.php index 11e4d020..6f3d239c 100644 --- a/Tests/EventListener/Doctrine/UploadListenerTest.php +++ b/Tests/EventListener/Doctrine/UploadListenerTest.php @@ -14,7 +14,7 @@ class UploadListenerTest extends ListenerTestCase /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -24,7 +24,7 @@ protected function setUp() /** * Test the getSubscribedEvents method. */ - public function testGetSubscribedEvents() + public function testGetSubscribedEvents(): void { $events = $this->listener->getSubscribedEvents(); @@ -34,7 +34,7 @@ public function testGetSubscribedEvents() /** * Tests the prePersist method. */ - public function testPrePersist() + public function testPrePersist(): void { $this->metadata ->expects($this->once()) @@ -61,7 +61,7 @@ public function testPrePersist() /** * Tests that prePersist skips non-uploadable entity. */ - public function testPrePersistSkipsNonUploadable() + public function testPrePersistSkipsNonUploadable(): void { $this->metadata ->expects($this->once()) @@ -79,7 +79,7 @@ public function testPrePersistSkipsNonUploadable() /** * Test the preUpdate method. */ - public function testPreUpdate() + public function testPreUpdate(): void { $this->adapter ->expects($this->once()) @@ -111,7 +111,7 @@ public function testPreUpdate() /** * Test that preUpdate skips non uploadable entity. */ - public function testPreUpdateSkipsNonUploadable() + public function testPreUpdateSkipsNonUploadable(): void { $this->metadata ->expects($this->once()) diff --git a/Tests/EventListener/Propel/CleanListenerTest.php b/Tests/EventListener/Propel/CleanListenerTest.php index a9c34bd3..201e83ae 100644 --- a/Tests/EventListener/Propel/CleanListenerTest.php +++ b/Tests/EventListener/Propel/CleanListenerTest.php @@ -14,7 +14,7 @@ class CleanListenerTest extends ListenerTestCase /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -24,14 +24,14 @@ protected function setUp() /** * Test the getSubscribedEvents method. */ - public function testGetSubscribedEvents() + public function testGetSubscribedEvents(): void { $events = $this->listener->getSubscribedEvents(); $this->assertArrayHasKey('propel.pre_update', $events); } - public function testOnUpload() + public function testOnUpload(): void { $this->handler ->expects($this->once()) diff --git a/Tests/EventListener/Propel/InjectListenerTest.php b/Tests/EventListener/Propel/InjectListenerTest.php index 93a90560..6ec0aa1c 100644 --- a/Tests/EventListener/Propel/InjectListenerTest.php +++ b/Tests/EventListener/Propel/InjectListenerTest.php @@ -14,7 +14,7 @@ class InjectListenerTest extends ListenerTestCase /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -24,14 +24,14 @@ protected function setUp() /** * Test the getSubscribedEvents method. */ - public function testGetSubscribedEvents() + public function testGetSubscribedEvents(): void { $events = $this->listener->getSubscribedEvents(); $this->assertArrayHasKey('propel.post_hydrate', $events); } - public function testOnHydrate() + public function testOnHydrate(): void { $this->handler ->expects($this->once()) diff --git a/Tests/EventListener/Propel/ListenerTestCase.php b/Tests/EventListener/Propel/ListenerTestCase.php index c101e887..89a74126 100644 --- a/Tests/EventListener/Propel/ListenerTestCase.php +++ b/Tests/EventListener/Propel/ListenerTestCase.php @@ -53,7 +53,7 @@ class ListenerTestCase extends TestCase /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { $this->adapter = $this->getAdapterMock(); $this->handler = $this->getHandlerMock(); diff --git a/Tests/EventListener/Propel/RemoveListenerTest.php b/Tests/EventListener/Propel/RemoveListenerTest.php index e652bde3..3ea53335 100644 --- a/Tests/EventListener/Propel/RemoveListenerTest.php +++ b/Tests/EventListener/Propel/RemoveListenerTest.php @@ -14,7 +14,7 @@ class RemoveListenerTest extends ListenerTestCase /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -24,14 +24,14 @@ protected function setUp() /** * Test the getSubscribedEvents method. */ - public function testGetSubscribedEvents() + public function testGetSubscribedEvents(): void { $events = $this->listener->getSubscribedEvents(); $this->assertArrayHasKey('propel.post_delete', $events); } - public function testOnDelete() + public function testOnDelete(): void { $this->handler ->expects($this->once()) diff --git a/Tests/EventListener/Propel/UploadListenerTest.php b/Tests/EventListener/Propel/UploadListenerTest.php index 6a8ecdef..d2a5d70f 100644 --- a/Tests/EventListener/Propel/UploadListenerTest.php +++ b/Tests/EventListener/Propel/UploadListenerTest.php @@ -14,7 +14,7 @@ class UploadListenerTest extends ListenerTestCase /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -24,7 +24,7 @@ protected function setUp() /** * Test the getSubscribedEvents method. */ - public function testGetSubscribedEvents() + public function testGetSubscribedEvents(): void { $events = $this->listener->getSubscribedEvents(); @@ -32,7 +32,7 @@ public function testGetSubscribedEvents() $this->assertArrayHasKey('propel.pre_insert', $events); } - public function testOnUpload() + public function testOnUpload(): void { $this->handler ->expects($this->once()) diff --git a/Tests/Fixtures/App/app/AppKernel.php b/Tests/Fixtures/App/app/AppKernel.php index 4a1cd6f7..3922cdfb 100644 --- a/Tests/Fixtures/App/app/AppKernel.php +++ b/Tests/Fixtures/App/app/AppKernel.php @@ -5,7 +5,7 @@ class AppKernel extends Kernel { - public function registerBundles() + public function registerBundles(): array { return [ new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), @@ -17,23 +17,17 @@ public function registerBundles() ]; } - public function registerContainerConfiguration(LoaderInterface $loader) + public function registerContainerConfiguration(LoaderInterface $loader): void { $loader->load(__DIR__.'/config/config.yml'); } - /** - * @return string - */ - public function getCacheDir() + public function getCacheDir(): string { return sys_get_temp_dir().'/VichUploaderBundle/cache'; } - /** - * @return string - */ - public function getLogDir() + public function getLogDir(): string { return sys_get_temp_dir().'/VichUploaderBundle/logs'; } diff --git a/Tests/Fixtures/App/src/TestBundle/Entity/Article.php b/Tests/Fixtures/App/src/TestBundle/Entity/Article.php index 6832a449..e5263be2 100644 --- a/Tests/Fixtures/App/src/TestBundle/Entity/Article.php +++ b/Tests/Fixtures/App/src/TestBundle/Entity/Article.php @@ -31,12 +31,12 @@ class Article protected $sizeField; - public function getAttachment() + public function getAttachment(): void { $this->attachment; } - public function setAttachment($attachment) + public function setAttachment($attachment): void { $this->attachment = $attachment; } @@ -46,17 +46,17 @@ public function getAttachmentName() return $this->attachmentName; } - public function setAttachmentName($attachmentName) + public function setAttachmentName($attachmentName): void { $this->attachmentName = $attachmentName; } - public function getImage() + public function getImage(): void { $this->image; } - public function setImage($image) + public function setImage($image): void { $this->image = $image; } @@ -66,7 +66,7 @@ public function getImageName() return $this->imageName; } - public function setImageName($imageName) + public function setImageName($imageName): void { $this->imageName = $imageName; } @@ -76,7 +76,7 @@ public function getOriginalNameField() return $this->originalNameField; } - public function setOriginalNameField($originalNameField) + public function setOriginalNameField($originalNameField): void { $this->originalNameField = $originalNameField; } @@ -86,7 +86,7 @@ public function getMimeTypeField() return $this->mimeTypeField; } - public function setMimeTypeField($mimeTypeField) + public function setMimeTypeField($mimeTypeField): void { $this->mimeTypeField = $mimeTypeField; } @@ -96,7 +96,7 @@ public function getSizeField() return $this->sizeField; } - public function setSizeField($sizeField) + public function setSizeField($sizeField): void { $this->sizeField = $sizeField; } diff --git a/Tests/Fixtures/App/src/TestBundle/Entity/Image.php b/Tests/Fixtures/App/src/TestBundle/Entity/Image.php index eaee9c55..3e7f0d3d 100644 --- a/Tests/Fixtures/App/src/TestBundle/Entity/Image.php +++ b/Tests/Fixtures/App/src/TestBundle/Entity/Image.php @@ -57,7 +57,7 @@ public function getId() * * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image */ - public function setImageFile(File $image = null) + public function setImageFile(File $image = null): void { $this->imageFile = $image; @@ -77,7 +77,7 @@ public function getImageFile() /** * @param string $imageName */ - public function setImageName($imageName) + public function setImageName($imageName): void { $this->imageName = $imageName; } @@ -93,7 +93,7 @@ public function getImageName() /** * @param string $title */ - public function setTitle($title) + public function setTitle($title): void { $this->title = $title; } diff --git a/Tests/Fixtures/App/src/TestBundle/Entity/Product.php b/Tests/Fixtures/App/src/TestBundle/Entity/Product.php index 965ede25..b0043f98 100644 --- a/Tests/Fixtures/App/src/TestBundle/Entity/Product.php +++ b/Tests/Fixtures/App/src/TestBundle/Entity/Product.php @@ -32,7 +32,7 @@ public function getImage() /** * @param File $image */ - public function setImage($image) + public function setImage($image): void { $this->image = $image; } @@ -42,7 +42,7 @@ public function getImageName() return $this->imageName; } - public function setImageName($imageName) + public function setImageName($imageName): void { $this->imageName = $imageName; } @@ -52,7 +52,7 @@ public function getImageSize() return $this->imageSize; } - public function setImageSize($imageSize) + public function setImageSize($imageSize): void { $this->imageSize = $imageSize; } @@ -62,7 +62,7 @@ public function getImageMimeType() return $this->imageMimeType; } - public function setImageMimeType($imageMimeType) + public function setImageMimeType($imageMimeType): void { $this->imageMimeType = $imageMimeType; } @@ -72,7 +72,7 @@ public function getImageOriginalName() return $this->imageOriginalName; } - public function setImageOriginalName($imageOriginalName) + public function setImageOriginalName($imageOriginalName): void { $this->imageOriginalName = $imageOriginalName; } @@ -82,7 +82,7 @@ public function getTitle() return $this->title; } - public function setTitle($title) + public function setTitle($title): void { $this->title = $title; } diff --git a/Tests/Form/Type/VichFileTypeTest.php b/Tests/Form/Type/VichFileTypeTest.php index b4e8b9fb..ae83aa9b 100644 --- a/Tests/Form/Type/VichFileTypeTest.php +++ b/Tests/Form/Type/VichFileTypeTest.php @@ -24,7 +24,7 @@ class VichFileTypeTest extends TestCase * @group legacy * @expectedDeprecation The "download_link" option is deprecated since version 1.6 and will be removed in 2.0. You should use "download_uri" instead. */ - public function testConfigureOptionsBC($options, $resolvedOptions) + public function testConfigureOptionsBC($options, $resolvedOptions): void { $optionsResolver = new OptionsResolver(); @@ -43,7 +43,7 @@ public function testConfigureOptionsBC($options, $resolvedOptions) $this->assertArraySubset($resolvedOptions, $resolved); } - public function configureOptionsBCDataProvider() + public function configureOptionsBCDataProvider(): array { return [ [['download_link' => true], ['download_uri' => true]], @@ -51,7 +51,7 @@ public function configureOptionsBCDataProvider() ]; } - public function testEmptyDownloadLinkDoNotThrowsDeprecation() + public function testEmptyDownloadLinkDoNotThrowsDeprecation(): void { $optionsResolver = new OptionsResolver(); @@ -73,7 +73,7 @@ public function testEmptyDownloadLinkDoNotThrowsDeprecation() /** * @dataProvider buildViewDataProvider */ - public function testBuildView(Product $object = null, array $options, array $vars) + public function testBuildView(Product $object = null, array $options, array $vars): void { $field = 'image'; @@ -138,7 +138,7 @@ public function testBuildView(Product $object = null, array $options, array $var $this->assertEquals($vars, $view->vars); } - public function buildViewDataProvider() + public function buildViewDataProvider(): array { $object = new Product(); $object->setImageOriginalName('image.jpeg'); diff --git a/Tests/Form/Type/VichImageTypeTest.php b/Tests/Form/Type/VichImageTypeTest.php index 9a504be6..18988fab 100644 --- a/Tests/Form/Type/VichImageTypeTest.php +++ b/Tests/Form/Type/VichImageTypeTest.php @@ -17,7 +17,7 @@ class VichImageTypeTest extends VichFileTypeTest { const TESTED_TYPE = VichImageType::class; - public function buildViewDataProvider() + public function buildViewDataProvider(): array { $object = new Product(); @@ -134,7 +134,7 @@ public function buildViewDataProvider() ]; } - public function testLiipImagineBundleIntegration() + public function testLiipImagineBundleIntegration(): void { if (!class_exists(CacheManager::class)) { $this->markTestSkipped('LiipImagineBundle is not installed.'); @@ -204,12 +204,11 @@ public function testLiipImagineBundleIntegration() $this->assertEquals($vars, $view->vars); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessage LiipImagineBundle must be installed and configured for using "imagine_pattern" option. - */ - public function testLiipImagineBundleIntegrationThrownExceptionIfNotAvailable() + public function testLiipImagineBundleIntegrationThrownExceptionIfNotAvailable(): void { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('LiipImagineBundle must be installed and configured for using "imagine_pattern" option.'); + $object = new Product(); $testedType = static::TESTED_TYPE; diff --git a/Tests/Functional/UploadTest.php b/Tests/Functional/UploadTest.php index e4d0f4ea..855e485d 100644 --- a/Tests/Functional/UploadTest.php +++ b/Tests/Functional/UploadTest.php @@ -4,7 +4,7 @@ class UploadTest extends WebTestCase { - public function testFileIsUploadedWithFileType() + public function testFileIsUploadedWithFileType(): void { $client = static::createClient(); $this->loadFixtures($client); @@ -42,7 +42,7 @@ public function testFileIsUploadedWithFileType() $this->assertFileNotExists($this->getUploadsDir($client).'/symfony_black_03.png', 'The file is deleted'); } - public function testFileIsUploadedWithImageType() + public function testFileIsUploadedWithImageType(): void { $client = static::createClient(); $this->loadFixtures($client); diff --git a/Tests/Functional/WebTestCase.php b/Tests/Functional/WebTestCase.php index 6e67ce5c..d3d3b13d 100644 --- a/Tests/Functional/WebTestCase.php +++ b/Tests/Functional/WebTestCase.php @@ -41,7 +41,7 @@ protected function getContainer($client) return $client->getKernel()->getContainer(); } - protected function loadFixtures($client) + protected function loadFixtures($client): void { $container = $this->getContainer($client); $registry = $container->get('doctrine'); diff --git a/Tests/Handler/DownloadHandlerTest.php b/Tests/Handler/DownloadHandlerTest.php index 439cf100..46a42c73 100644 --- a/Tests/Handler/DownloadHandlerTest.php +++ b/Tests/Handler/DownloadHandlerTest.php @@ -29,7 +29,7 @@ class DownloadHandlerTest extends TestCase protected $mapping; - protected function setUp() + protected function setUp(): void { $this->factory = $this->getPropertyMappingFactoryMock(); $this->storage = $this->createMock(StorageInterface::class); @@ -57,7 +57,7 @@ public function filenamesProvider() /** * @dataProvider filenamesProvider */ - public function testDownloadObject($fileName, $expectedFileName) + public function testDownloadObject($fileName, $expectedFileName): void { $file = $this->getUploadedFileMock(); @@ -93,7 +93,7 @@ public function testDownloadObject($fileName, $expectedFileName) /** * @dataProvider filenamesProvider */ - public function testDownloadObjectWithoutFile($fileName, $expectedFileName) + public function testDownloadObjectWithoutFile($fileName, $expectedFileName): void { $this->mapping ->expects($this->once()) @@ -119,7 +119,7 @@ public function testDownloadObjectWithoutFile($fileName, $expectedFileName) $this->assertSame(sprintf('attachment; filename="%s"', $expectedFileName), $response->headers->get('Content-Disposition')); } - public function testDownloadObjectCallOriginalName() + public function testDownloadObjectCallOriginalName(): void { $this->object->setImageOriginalName('original-name.jpeg'); @@ -157,7 +157,7 @@ public function testDownloadObjectCallOriginalName() ); } - public function testNonAsciiFilenameIsTransliterated() + public function testNonAsciiFilenameIsTransliterated(): void { $file = $this->getUploadedFileMock(); @@ -183,22 +183,20 @@ public function testNonAsciiFilenameIsTransliterated() $this->assertInstanceOf(StreamedResponse::class, $response); } - /** - * @expectedException \Vich\UploaderBundle\Exception\MappingNotFoundException - */ - public function testAnExceptionIsThrownIfMappingIsNotFound() + public function testAnExceptionIsThrownIfMappingIsNotFound(): void { + $this->expectException(\Vich\UploaderBundle\Exception\MappingNotFoundException::class); + $this->factory = $this->getPropertyMappingFactoryMock(); $this->handler = new DownloadHandler($this->factory, $this->storage); $this->handler->downloadObject($this->object, 'file_field'); } - /** - * @expectedException \Vich\UploaderBundle\Exception\NoFileFoundException - */ - public function testAnExceptionIsThrownIfNoFileIsFound() + public function testAnExceptionIsThrownIfNoFileIsFound(): void { + $this->expectException(\Vich\UploaderBundle\Exception\NoFileFoundException::class); + $this->storage ->expects($this->once()) ->method('resolveStream') diff --git a/Tests/Handler/UploadHandlerTest.php b/Tests/Handler/UploadHandlerTest.php index 911165f9..4f816501 100644 --- a/Tests/Handler/UploadHandlerTest.php +++ b/Tests/Handler/UploadHandlerTest.php @@ -32,7 +32,7 @@ class UploadHandlerTest extends TestCase const FILE_FIELD = 'image'; - protected function setUp() + protected function setUp(): void { $this->factory = $this->getPropertyMappingFactoryMock(); $this->storage = $this->getStorageMock(); @@ -49,7 +49,7 @@ protected function setUp() ->will($this->returnValue($this->mapping)); } - public function testUpload() + public function testUpload(): void { $this->expectEvents([Events::PRE_UPLOAD, Events::POST_UPLOAD]); @@ -74,10 +74,11 @@ public function testUpload() /** * @dataProvider methodProvider - * @expectedException \Vich\UploaderBundle\Exception\MappingNotFoundException */ - public function testAnExceptionIsThrownIfMappingIsntFound($method) + public function testAnExceptionIsThrownIfMappingIsntFound($method): void { + $this->expectException(\Vich\UploaderBundle\Exception\MappingNotFoundException::class); + $this->factory = $this->getPropertyMappingFactoryMock(); $handler = new UploadHandler($this->factory, $this->storage, $this->injector, $this->dispatcher); @@ -94,7 +95,7 @@ public function methodProvider() ]; } - public function testUploadSkipsEmptyObjects() + public function testUploadSkipsEmptyObjects(): void { $this->dispatcher ->expects($this->never()) @@ -111,7 +112,7 @@ public function testUploadSkipsEmptyObjects() $this->handler->upload($this->object, self::FILE_FIELD); } - public function testInject() + public function testInject(): void { $this->expectEvents([Events::PRE_INJECT, Events::POST_INJECT]); @@ -123,7 +124,7 @@ public function testInject() $this->handler->inject($this->object, self::FILE_FIELD); } - public function testClean() + public function testClean(): void { $this->expectEvents([Events::PRE_REMOVE, Events::POST_REMOVE]); @@ -147,7 +148,7 @@ public function testClean() $this->handler->clean($this->object, self::FILE_FIELD); } - public function testCleanSkipsEmptyObjects() + public function testCleanSkipsEmptyObjects(): void { $this->mapping ->expects($this->any()) @@ -166,7 +167,7 @@ public function testCleanSkipsEmptyObjects() $this->handler->clean($this->object, self::FILE_FIELD); } - public function testRemove() + public function testRemove(): void { $this->expectEvents([Events::PRE_REMOVE, Events::POST_REMOVE]); @@ -190,7 +191,7 @@ public function testRemove() $this->handler->remove($this->object, self::FILE_FIELD); } - public function testRemoveWithEmptyObject() + public function testRemoveWithEmptyObject(): void { $this->dispatcher ->expects($this->never()) @@ -229,7 +230,7 @@ protected function validEvent() }); } - protected function expectEvents(array $events) + protected function expectEvents(array $events): void { foreach ($events as $i => $event) { $this->dispatcher diff --git a/Tests/Injector/FileInjectorTest.php b/Tests/Injector/FileInjectorTest.php index ae224d6c..2877188c 100644 --- a/Tests/Injector/FileInjectorTest.php +++ b/Tests/Injector/FileInjectorTest.php @@ -21,7 +21,7 @@ class FileInjectorTest extends TestCase /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { $this->storage = $this->getMockStorage(); } @@ -29,7 +29,7 @@ protected function setUp() /** * Test inject one file. */ - public function testInjectsOneFile() + public function testInjectsOneFile(): void { $obj = $this->createMock('Vich\UploaderBundle\Tests\DummyEntity'); @@ -58,7 +58,7 @@ public function testInjectsOneFile() * Test that if the file name property returns a null value * then no file is injected. */ - public function testPropertyIsNullWhenFileNamePropertyIsNull() + public function testPropertyIsNullWhenFileNamePropertyIsNull(): void { $obj = $this->createMock('Vich\UploaderBundle\Tests\DummyEntity'); diff --git a/Tests/Mapping/Annotation/UploadableFieldTest.php b/Tests/Mapping/Annotation/UploadableFieldTest.php index 01e35893..db3141b6 100644 --- a/Tests/Mapping/Annotation/UploadableFieldTest.php +++ b/Tests/Mapping/Annotation/UploadableFieldTest.php @@ -12,11 +12,10 @@ */ class UploadableFieldTest extends TestCase { - /** - * @expectedException \InvalidArgumentException - */ - public function testExceptionThrownWhenNoMappingAttribute() + public function testExceptionThrownWhenNoMappingAttribute(): void { + $this->expectException(\InvalidArgumentException::class); + new UploadableField([ 'fileNameProperty' => 'fileName', ]); diff --git a/Tests/Mapping/PropertyMappingFactoryTest.php b/Tests/Mapping/PropertyMappingFactoryTest.php index 9c0c7c2d..bb3b3258 100644 --- a/Tests/Mapping/PropertyMappingFactoryTest.php +++ b/Tests/Mapping/PropertyMappingFactoryTest.php @@ -28,7 +28,7 @@ class PropertyMappingFactoryTest extends TestCase /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { $this->container = $this->getContainerMock(); $this->metadata = $this->getMetadataReaderMock(); @@ -37,11 +37,11 @@ protected function setUp() /** * Tests that an exception is thrown if a non uploadable * object is passed in. - * - * @expectedException \Vich\UploaderBundle\Exception\NotUploadableException */ - public function testFromObjectThrowsExceptionIfNotUploadable() + public function testFromObjectThrowsExceptionIfNotUploadable(): void { + $this->expectException(\Vich\UploaderBundle\Exception\NotUploadableException::class); + $this->metadata ->expects($this->once()) ->method('isUploadable') @@ -57,7 +57,7 @@ public function testFromObjectThrowsExceptionIfNotUploadable() * * @dataProvider fromObjectProvider */ - public function testFromObjectOneField($object, $givenClassName, $expectedClassName) + public function testFromObjectOneField($object, $givenClassName, $expectedClassName): void { $mappings = [ 'dummy_file' => [ @@ -111,16 +111,15 @@ public function fromObjectProvider() ]; } - /** - * @expectedException \RuntimeException - */ - public function testMappingCreationFailsIfTheClassNameCannotBeDetermined() + public function testMappingCreationFailsIfTheClassNameCannotBeDetermined(): void { + $this->expectException(\RuntimeException::class); + $factory = new PropertyMappingFactory($this->container, $this->metadata, []); $factory->fromObject([]); } - public function testFromObjectOneFieldWithNoExplicitFilenameProperty() + public function testFromObjectOneFieldWithNoExplicitFilenameProperty(): void { $obj = new DummyEntity(); @@ -163,7 +162,7 @@ public function testFromObjectOneFieldWithNoExplicitFilenameProperty() $this->assertEquals('file_name', $mapping->getFileNamePropertyName()); } - public function testFromObjectWithExplicitMapping() + public function testFromObjectWithExplicitMapping(): void { $mappings = [ 'dummy_mapping' => [ @@ -216,11 +215,11 @@ public function testFromObjectWithExplicitMapping() /** * Test that an exception is thrown when an invalid mapping name * is specified. - * - * @expectedException \Vich\UploaderBundle\Exception\MappingNotFoundException */ - public function testThrowsExceptionOnInvalidMappingName() + public function testThrowsExceptionOnInvalidMappingName(): void { + $this->expectException(\Vich\UploaderBundle\Exception\MappingNotFoundException::class); + $mappings = [ 'bad_name' => [], ]; @@ -250,7 +249,7 @@ public function testThrowsExceptionOnInvalidMappingName() /** * @dataProvider fromFieldProvider */ - public function testFromField($object, $className, $expectedClassName) + public function testFromField($object, $className, $expectedClassName): void { $mappings = [ 'dummy_file' => [ @@ -300,7 +299,7 @@ public function fromFieldProvider() * Test that the fromField method returns null when an invalid * field name is specified. */ - public function testFromFieldReturnsNullOnInvalidFieldName() + public function testFromFieldReturnsNullOnInvalidFieldName(): void { $this->metadata ->expects($this->once()) @@ -320,7 +319,7 @@ public function testFromFieldReturnsNullOnInvalidFieldName() $this->assertNull($mapping); } - public function testCustomFileNameProperty() + public function testCustomFileNameProperty(): void { $mappings = [ 'dummy_file' => [ @@ -350,7 +349,7 @@ public function testCustomFileNameProperty() $this->assertEquals('file_suffix', $mapping->getFileNamePropertyName()); } - public function testConfiguredNamersAreRetrievedFromContainer() + public function testConfiguredNamersAreRetrievedFromContainer(): void { $mappings = [ 'dummy_file' => [ diff --git a/Tests/Mapping/PropertyMappingTest.php b/Tests/Mapping/PropertyMappingTest.php index c899f515..62c811e8 100644 --- a/Tests/Mapping/PropertyMappingTest.php +++ b/Tests/Mapping/PropertyMappingTest.php @@ -19,7 +19,7 @@ class PropertyMappingTest extends TestCase * Test that the configured mappings are accessed * correctly. */ - public function testConfiguredMappingAccess() + public function testConfiguredMappingAccess(): void { $object = new DummyEntity(); $prop = new PropertyMapping('file', 'fileName'); @@ -33,52 +33,10 @@ public function testConfiguredMappingAccess() $this->assertEquals('fileName', $prop->getFileNamePropertyName()); } - /** - * @dataProvider propertiesAccessProvider - */ - public function testPropertiesAreAccessed($object, $file, $fileName) - { - $prop = new PropertyMapping('file', 'fileName'); - - $this->assertSame($file, $prop->getFile($object)); - $this->assertSame($fileName, $prop->getFileName($object)); - } - - public function propertiesAccessProvider() - { - $date = new \DateTime(); - $object = new DummyEntity(); - $object->setFileName('joe.png'); - $object->setFile($date); - - $array = [ - 'fileName' => 'joe.png', - 'file' => $date, - ]; - - return [ - [$object, $date, 'joe.png'], - [$array, $date, 'joe.png'], - ]; - } - - public function testPropertiesAreSet() - { - $date = new \DateTime(); - $object = new DummyEntity(); - - $prop = new PropertyMapping('file', 'fileName'); - $prop->setFile($object, $date); - $prop->setFileName($object, 'joe.png'); - - $this->assertSame($date, $object->getFile()); - $this->assertSame('joe.png', $object->getFileName()); - } - /** * @dataProvider directoryProvider */ - public function testDirectoryNamerIsCalled($dir, $expectedDir) + public function testDirectoryNamerIsCalled($dir, $expectedDir): void { $object = new DummyEntity(); $prop = new PropertyMapping('file', 'fileName'); @@ -99,7 +57,7 @@ public function testDirectoryNamerIsCalled($dir, $expectedDir) $this->assertEquals('/tmp', $prop->getUploadDestination()); } - public function testReadProperty() + public function testReadProperty(): void { $object = new DummyEntity(); $object->setSize(100); @@ -108,18 +66,17 @@ public function testReadProperty() $this->assertEquals(100, $prop->readProperty($object, 'size')); } - /** - * @expectedException \InvalidArgumentException - */ - public function testReadUnknownProperty() + public function testReadUnknownProperty(): void { + $this->expectException(\InvalidArgumentException::class); + $object = new DummyEntity(); $prop = new PropertyMapping('file', 'fileName'); $prop->readProperty($object, 'unused'); } - public function testWriteProperty() + public function testWriteProperty(): void { $object = new DummyEntity(); $prop = new PropertyMapping('file', 'fileName', ['size' => 'size']); @@ -128,18 +85,17 @@ public function testWriteProperty() $this->assertEquals(100, $object->getSize()); } - /** - * @expectedException \InvalidArgumentException - */ - public function testWriteUnknownProperty() + public function testWriteUnknownProperty(): void { + $this->expectException(\InvalidArgumentException::class); + $object = new DummyEntity(); $prop = new PropertyMapping('file', 'fileName'); $prop->writeProperty($object, 'unused', null); } - public function testGetUploadNameWithNamer() + public function testGetUploadNameWithNamer(): void { $object = new DummyEntity(); $prop = new PropertyMapping('file', 'fileName'); @@ -156,7 +112,7 @@ public function testGetUploadNameWithNamer() $this->assertEquals('123', $prop->getUploadName($object)); } - public function testGetUploadNameWithoutNamer() + public function testGetUploadNameWithoutNamer(): void { $object = new DummyEntity(); $prop = new PropertyMapping('file', 'fileName'); @@ -172,7 +128,7 @@ public function testGetUploadNameWithoutNamer() $this->assertEquals('filename', $prop->getUploadName($object)); } - public function directoryProvider() + public function directoryProvider(): array { return [ ['other_dir', 'other_dir'], @@ -183,7 +139,7 @@ public function directoryProvider() ]; } - public function testErase() + public function testErase(): void { $object = new Article(); diff --git a/Tests/Metadata/ClassMetadataTest.php b/Tests/Metadata/ClassMetadataTest.php index cb32d53d..56ecd1de 100644 --- a/Tests/Metadata/ClassMetadataTest.php +++ b/Tests/Metadata/ClassMetadataTest.php @@ -7,7 +7,7 @@ class ClassMetadataTest extends TestCase { - public function testFieldsAreSerialized() + public function testFieldsAreSerialized(): void { $fields = ['foo', 'bar', 'baz']; $metadata = new ClassMetadata('DateTime'); diff --git a/Tests/Metadata/Driver/AnnotationDriverTest.php b/Tests/Metadata/Driver/AnnotationDriverTest.php index 5e2e5b87..55fc6f73 100644 --- a/Tests/Metadata/Driver/AnnotationDriverTest.php +++ b/Tests/Metadata/Driver/AnnotationDriverTest.php @@ -15,7 +15,7 @@ */ class AnnotationDriverTest extends TestCase { - public function testReadUploadableAnnotation() + public function testReadUploadableAnnotation(): void { $entity = new DummyEntity(); @@ -50,7 +50,7 @@ public function testReadUploadableAnnotation() ], $metadata->fields); } - public function testReadUploadableAnnotationReturnsNullWhenNonePresent() + public function testReadUploadableAnnotationReturnsNullWhenNonePresent(): void { $entity = new DummyEntity(); @@ -69,7 +69,7 @@ public function testReadUploadableAnnotationReturnsNullWhenNonePresent() $this->assertNull($metadata); } - public function testReadTwoUploadableFields() + public function testReadTwoUploadableFields(): void { $entity = new Article(); @@ -122,7 +122,7 @@ public function testReadTwoUploadableFields() ], $metadata->fields); } - public function testReadNoUploadableFieldsWhenNoneExist() + public function testReadNoUploadableFieldsWhenNoneExist(): void { $entity = new DummyEntity(); diff --git a/Tests/Metadata/Driver/FileDriverTestCase.php b/Tests/Metadata/Driver/FileDriverTestCase.php index fce061d2..d63b1a98 100644 --- a/Tests/Metadata/Driver/FileDriverTestCase.php +++ b/Tests/Metadata/Driver/FileDriverTestCase.php @@ -14,7 +14,7 @@ abstract class FileDriverTestCase extends TestCase /** * @dataProvider classesProvider */ - public function testLoadMetadataForClass($class, $file, $expectedMetadata) + public function testLoadMetadataForClass($class, $file, $expectedMetadata): void { $reflectionClass = new \ReflectionClass($class); $driver = $this->getDriver($reflectionClass, $file); diff --git a/Tests/Metadata/Driver/YamlDriverTest.php b/Tests/Metadata/Driver/YamlDriverTest.php index ee859e7e..5a70da96 100644 --- a/Tests/Metadata/Driver/YamlDriverTest.php +++ b/Tests/Metadata/Driver/YamlDriverTest.php @@ -10,11 +10,10 @@ */ class YamlDriverTest extends FileDriverTestCase { - /** - * @expectedException \RuntimeException - */ - public function testInconsistentYamlFile() + public function testInconsistentYamlFile(): void { + $this->expectException(\RuntimeException::class); + $rClass = new \ReflectionClass(\DateTime::class); $fileLocator = $this->createMock(FileLocatorInterface::class); diff --git a/Tests/Metadata/MetadataReaderTest.php b/Tests/Metadata/MetadataReaderTest.php index 134e0a57..e740b899 100644 --- a/Tests/Metadata/MetadataReaderTest.php +++ b/Tests/Metadata/MetadataReaderTest.php @@ -11,13 +11,13 @@ class MetadataReaderTest extends TestCase protected $factory; - protected function setUp() + protected function setUp(): void { $this->factory = $this->createMock('Metadata\AdvancedMetadataFactoryInterface'); $this->reader = new MetadataReader($this->factory); } - public function testIsUploadable() + public function testIsUploadable(): void { $this->factory ->expects($this->once()) @@ -28,7 +28,7 @@ public function testIsUploadable() $this->assertTrue($this->reader->isUploadable('ClassName')); } - public function testIsUploadableWithGivenMapping() + public function testIsUploadableWithGivenMapping(): void { $fields = ['field' => ['mapping' => 'joe']]; $classMetadata = new \stdClass(); @@ -45,7 +45,7 @@ public function testIsUploadableWithGivenMapping() $this->assertFalse($this->reader->isUploadable('ClassName', 'foo')); } - public function testIsUploadableForNotUploadable() + public function testIsUploadableForNotUploadable(): void { $this->factory ->expects($this->once()) @@ -56,7 +56,7 @@ public function testIsUploadableForNotUploadable() $this->assertFalse($this->reader->isUploadable('ClassName')); } - public function testGetUploadableClassesForwardsCallsToTheFactory() + public function testGetUploadableClassesForwardsCallsToTheFactory(): void { $this->factory ->expects($this->once()) @@ -65,7 +65,7 @@ public function testGetUploadableClassesForwardsCallsToTheFactory() $this->reader->getUploadableClasses(); } - public function testGetUploadableFields() + public function testGetUploadableFields(): void { $fields = [ 'foo' => ['mapping' => 'foo_mapping'], @@ -89,7 +89,7 @@ public function testGetUploadableFields() $this->assertSame($barFields, $this->reader->getUploadableFields('ClassName', 'bar_mapping')); } - public function testGetUploadableFieldsWithInheritance() + public function testGetUploadableFieldsWithInheritance(): void { $classMetadata = new \stdClass(); $classMetadata->fields = ['bar', 'baz']; @@ -113,7 +113,7 @@ public function testGetUploadableFieldsWithInheritance() /** * @dataProvider fieldsMetadataProvider */ - public function testGetUploadableField(array $fields, $expectedMetadata) + public function testGetUploadableField(array $fields, $expectedMetadata): void { $classMetadata = new \stdClass(); $classMetadata->fields = $fields; @@ -129,21 +129,19 @@ public function testGetUploadableField(array $fields, $expectedMetadata) $this->assertSame($expectedMetadata, $this->reader->getUploadableField('ClassName', 'field')); } - /** - * @expectedException \Vich\UploaderBundle\Exception\MappingNotFoundException - * @expectedExceptionMessage Mapping not found. The configuration for the class "InvalidClassName" is probably incorrect. - */ - public function testGetUploadableFieldWithInvalidClass() + public function testGetUploadableFieldWithInvalidClass(): void { + $this->expectException(\Vich\UploaderBundle\Exception\MappingNotFoundException::class); + $this->expectExceptionMessage('Mapping not found. The configuration for the class "InvalidClassName" is probably incorrect.'); + $this->reader->getUploadableFields('InvalidClassName'); } - /** - * @expectedException \Vich\UploaderBundle\Exception\MappingNotFoundException - * @expectedExceptionMessage Mapping "foo_mapping" does not exist. The configuration for the class "InvalidClassName" is probably incorrect. - */ - public function testGetUploadableFieldWithInvalidClassMapping() + public function testGetUploadableFieldWithInvalidClassMapping(): void { + $this->expectException(\Vich\UploaderBundle\Exception\MappingNotFoundException::class); + $this->expectExceptionMessage('Mapping "foo_mapping" does not exist. The configuration for the class "InvalidClassName" is probably incorrect.'); + $this->reader->getUploadableFields('InvalidClassName', 'foo_mapping'); } diff --git a/Tests/Naming/Base64NamerTest.php b/Tests/Naming/Base64NamerTest.php index 1e6adcfc..6f869a00 100644 --- a/Tests/Naming/Base64NamerTest.php +++ b/Tests/Naming/Base64NamerTest.php @@ -7,7 +7,7 @@ class Base64Namer extends \Vich\UploaderBundle\Naming\Base64Namer { - protected function getRandomChar() + protected function getRandomChar(): string { return 'a'; } @@ -18,7 +18,7 @@ protected function getRandomChar() */ class Base64NamerTest extends TestCase { - public function fileDataProvider() + public function fileDataProvider(): array { return [ ['aaaaaaaaaa.jpg', 'jpg', null], @@ -31,7 +31,7 @@ public function fileDataProvider() /** * @dataProvider fileDataProvider */ - public function testNameReturnsTheRightName($expectedFileName, $extension, $length) + public function testNameReturnsTheRightName(string $expectedFileName, string $extension, ?int $length): void { $file = $this->getUploadedFileMock(); $file->expects($this->once()) diff --git a/Tests/Naming/HashNamerTest.php b/Tests/Naming/HashNamerTest.php index 77f1d4cd..89f47c12 100644 --- a/Tests/Naming/HashNamerTest.php +++ b/Tests/Naming/HashNamerTest.php @@ -8,7 +8,7 @@ class HashNamer extends BaseHashNamer { - protected function getRandomString() + protected function getRandomString(): string { return 'abcd'; } @@ -32,7 +32,7 @@ public function fileDataProvider() /** * @dataProvider fileDataProvider */ - public function testNameReturnsTheRightName($expectedFileName, $extension, $algorithm, $length) + public function testNameReturnsTheRightName($expectedFileName, $extension, $algorithm, $length): void { $file = $this->getUploadedFileMock(); $file->expects($this->once()) diff --git a/Tests/Naming/OrignameNamerTest.php b/Tests/Naming/OrignameNamerTest.php index ccc4a964..6d34bbdd 100644 --- a/Tests/Naming/OrignameNamerTest.php +++ b/Tests/Naming/OrignameNamerTest.php @@ -24,7 +24,7 @@ public function fileDataProvider() /** * @dataProvider fileDataProvider */ - public function testNameReturnsAnUniqueName($name, $pattern, $transliterate) + public function testNameReturnsAnUniqueName($name, $pattern, $transliterate): void { $file = $this->getUploadedFileMock(); $file diff --git a/Tests/Naming/PropertyDirectoryNamerTest.php b/Tests/Naming/PropertyDirectoryNamerTest.php index 0e49da43..4a1bfd9c 100644 --- a/Tests/Naming/PropertyDirectoryNamerTest.php +++ b/Tests/Naming/PropertyDirectoryNamerTest.php @@ -31,7 +31,7 @@ public function fileDataProvider() /** * @dataProvider fileDataProvider */ - public function testNameReturnsTheRightName($expectedDirectoryName, $entity, $propertyName, $transliterate) + public function testNameReturnsTheRightName($expectedDirectoryName, $entity, $propertyName, $transliterate): void { $mapping = $this->getPropertyMappingMock(); @@ -41,11 +41,10 @@ public function testNameReturnsTheRightName($expectedDirectoryName, $entity, $pr $this->assertSame($expectedDirectoryName, $namer->directoryName($entity, $mapping)); } - /** - * @expectedException \Vich\UploaderBundle\Exception\NameGenerationException - */ - public function testNameFailsIfThePropertyDoesNotExist() + public function testNameFailsIfThePropertyDoesNotExist(): void { + $this->expectException(\Vich\UploaderBundle\Exception\NameGenerationException::class); + $entity = new DummyEntity(); $mapping = $this->getPropertyMappingMock(); @@ -55,11 +54,10 @@ public function testNameFailsIfThePropertyDoesNotExist() $namer->directoryName($entity, $mapping); } - /** - * @expectedException \Vich\UploaderBundle\Exception\NameGenerationException - */ - public function testNameFailsIfThePropertyIsEmpty() + public function testNameFailsIfThePropertyIsEmpty(): void { + $this->expectException(\Vich\UploaderBundle\Exception\NameGenerationException::class); + $mapping = $this->getPropertyMappingMock(); $namer = new PropertyDirectoryNamer(); @@ -68,24 +66,22 @@ public function testNameFailsIfThePropertyIsEmpty() $namer->directoryName(new DummyEntity(), $mapping); } - /** - * @expectedException \LogicException - * @expectedExceptionMessage The property to use can not be determined. Did you call the configure() method? - */ - public function testNamerNeedsToBeConfigured() + public function testNamerNeedsToBeConfigured(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('The property to use can not be determined. Did you call the configure() method?'); + $mapping = $this->getPropertyMappingMock(); $namer = new PropertyDirectoryNamer(); $namer->directoryName(new DummyEntity(), $mapping); } - /** - * @expectedException \LogicException - * @expectedExceptionMessage Option "property" is missing or empty. - */ - public function testConfigurationFailsIfThePropertyIsntSpecified() + public function testConfigurationFailsIfThePropertyIsntSpecified(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Option "property" is missing or empty.'); + $namer = new PropertyDirectoryNamer(); $namer->configure(['incorrect' => 'options']); diff --git a/Tests/Naming/PropertyNamerTest.php b/Tests/Naming/PropertyNamerTest.php index a92d7c10..38b1f935 100644 --- a/Tests/Naming/PropertyNamerTest.php +++ b/Tests/Naming/PropertyNamerTest.php @@ -32,7 +32,7 @@ public function fileDataProvider() /** * @dataProvider fileDataProvider */ - public function testNameReturnsTheRightName($originalFileName, $expectedFileName, $entity, $propertyName, $transliterate) + public function testNameReturnsTheRightName($originalFileName, $expectedFileName, $entity, $propertyName, $transliterate): void { $file = $this->getUploadedFileMock(); $file @@ -52,11 +52,10 @@ public function testNameReturnsTheRightName($originalFileName, $expectedFileName $this->assertSame($expectedFileName, $namer->name($entity, $mapping)); } - /** - * @expectedException \Vich\UploaderBundle\Exception\NameGenerationException - */ - public function testNameFailsIfThePropertyDoesNotExist() + public function testNameFailsIfThePropertyDoesNotExist(): void { + $this->expectException(\Vich\UploaderBundle\Exception\NameGenerationException::class); + $entity = new DummyEntity(); $mapping = $this->getPropertyMappingMock(); @@ -66,11 +65,10 @@ public function testNameFailsIfThePropertyDoesNotExist() $namer->name($entity, $mapping); } - /** - * @expectedException \Vich\UploaderBundle\Exception\NameGenerationException - */ - public function testNameFailsIfThePropertyIsEmpty() + public function testNameFailsIfThePropertyIsEmpty(): void { + $this->expectException(\Vich\UploaderBundle\Exception\NameGenerationException::class); + $mapping = $this->getPropertyMappingMock(); $namer = new PropertyNamer(); @@ -79,24 +77,22 @@ public function testNameFailsIfThePropertyIsEmpty() $namer->name(new DummyEntity(), $mapping); } - /** - * @expectedException \LogicException - * @expectedExceptionMessage The property to use can not be determined. Did you call the configure() method? - */ - public function testNamerNeedsToBeConfigured() + public function testNamerNeedsToBeConfigured(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('The property to use can not be determined. Did you call the configure() method?'); + $mapping = $this->getPropertyMappingMock(); $namer = new PropertyNamer(); $namer->name(new DummyEntity(), $mapping); } - /** - * @expectedException \LogicException - * @expectedExceptionMessage Option "property" is missing or empty. - */ - public function testConfigurationFailsIfThePropertyIsntSpecified() + public function testConfigurationFailsIfThePropertyIsntSpecified(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Option "property" is missing or empty.'); + $namer = new PropertyNamer(); $namer->configure(['incorrect' => 'options']); diff --git a/Tests/Naming/SubdirDirectoryNamerTest.php b/Tests/Naming/SubdirDirectoryNamerTest.php index 473dea9c..6b9096c0 100644 --- a/Tests/Naming/SubdirDirectoryNamerTest.php +++ b/Tests/Naming/SubdirDirectoryNamerTest.php @@ -25,7 +25,7 @@ public function fileDataProvider() /** * @dataProvider fileDataProvider */ - public function testNameReturnsTheRightName($fileName, $expectedFileName, $charsPerDir, $dirs) + public function testNameReturnsTheRightName($fileName, $expectedFileName, $charsPerDir, $dirs): void { $entity = new DummyEntity(); $entity->setFileName($fileName); diff --git a/Tests/Naming/UniqidNamerTest.php b/Tests/Naming/UniqidNamerTest.php index 30a7e866..9ed5ddc8 100644 --- a/Tests/Naming/UniqidNamerTest.php +++ b/Tests/Naming/UniqidNamerTest.php @@ -26,7 +26,7 @@ public function fileDataProvider() /** * @dataProvider fileDataProvider */ - public function testNameReturnsAnUniqueName($originalName, $guessedExtension, $pattern) + public function testNameReturnsAnUniqueName($originalName, $guessedExtension, $pattern): void { $file = $this->getUploadedFileMock(); $file diff --git a/Tests/Storage/FileSystemStorageTest.php b/Tests/Storage/FileSystemStorageTest.php index d44e7cee..33f862b4 100644 --- a/Tests/Storage/FileSystemStorageTest.php +++ b/Tests/Storage/FileSystemStorageTest.php @@ -19,43 +19,13 @@ protected function getStorage() return new FileSystemStorage($this->factory); } - /** - * Tests the upload method skips a mapping which has a non - * uploadable property value. - * - * @expectedException \LogicException - * @dataProvider invalidFileProvider - * @group upload - */ - public function testUploadSkipsMappingOnInvalid($file) - { - $this->mapping - ->expects($this->once()) - ->method('getFile') - ->will($this->returnValue($file)); - - $this->mapping - ->expects($this->never()) - ->method('hasNamer'); - - $this->mapping - ->expects($this->never()) - ->method('getNamer'); - - $this->mapping - ->expects($this->never()) - ->method('getFileName'); - - $this->storage->upload($this->object, $this->mapping); - } - /** * Test the remove method skips trying to remove a file whose file name * property value returns null. * * @dataProvider emptyFilenameProvider */ - public function testRemoveSkipsEmptyFilenameProperties($propertyValue) + public function testRemoveSkipsEmptyFilenameProperties($propertyValue): void { $this->mapping ->expects($this->once()) @@ -72,7 +42,7 @@ public function testRemoveSkipsEmptyFilenameProperties($propertyValue) /** * Test the remove method skips trying to remove a file that no longer exists. */ - public function testRemoveSkipsNonExistingFile() + public function testRemoveSkipsNonExistingFile(): void { $this->mapping ->expects($this->once()) @@ -87,7 +57,7 @@ public function testRemoveSkipsNonExistingFile() $this->storage->remove($this->object, $this->mapping); } - public function testRemove() + public function testRemove(): void { $this->mapping ->expects($this->once()) @@ -106,7 +76,7 @@ public function testRemove() /** * Test the resolve path method. */ - public function testResolvePath() + public function testResolvePath(): void { $this->mapping ->expects($this->once()) @@ -137,7 +107,7 @@ public function testResolvePath() /** * Test the resolve path method. */ - public function testResolveRelativePath() + public function testResolveRelativePath(): void { $this->mapping ->expects($this->once()) @@ -160,7 +130,7 @@ public function testResolveRelativePath() $this->assertEquals(sprintf('upload_dir%sfile.txt', DIRECTORY_SEPARATOR), $path); } - public function testResolveUriReturnsNullIfNoFile() + public function testResolveUriReturnsNullIfNoFile(): void { $this->mapping ->expects($this->once()) @@ -181,7 +151,7 @@ public function testResolveUriReturnsNullIfNoFile() * * @dataProvider resolveUriDataProvider */ - public function testResolveUri($uploadDir, $uri) + public function testResolveUri($uploadDir, $uri): void { $this->mapping ->expects($this->once()) @@ -209,7 +179,7 @@ public function testResolveUri($uploadDir, $uri) $this->assertEquals($uri, $path); } - public function testResolveStream() + public function testResolveStream(): void { $this->mapping ->expects($this->once()) @@ -263,7 +233,7 @@ public function resolveUriDataProvider() * @dataProvider filenameWithDirectoriesDataProvider * @group upload */ - public function testUploadedFileIsCorrectlyMoved($uploadDir, $dir, $expectedDir) + public function testUploadedFileIsCorrectlyMoved($uploadDir, $dir, $expectedDir): void { $file = $this->getUploadedFileMock(); diff --git a/Tests/Storage/FlysystemStorageTest.php b/Tests/Storage/FlysystemStorageTest.php index d2b5ac3c..f1cadd9a 100644 --- a/Tests/Storage/FlysystemStorageTest.php +++ b/Tests/Storage/FlysystemStorageTest.php @@ -25,7 +25,7 @@ class FlysystemStorageTest extends StorageTestCase */ protected $filesystem; - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { if (!class_exists('League\Flysystem\MountManager')) { self::markTestSkipped('Flysystem is not installed.'); @@ -43,7 +43,7 @@ protected function getStorage() /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { $this->mountManager = $this->getMountManagerMock(); $this->filesystem = $this->createMock('League\Flysystem\FilesystemInterface'); @@ -62,7 +62,7 @@ protected function setUp() ->will($this->returnValue(self::FS_KEY)); } - public function testUpload() + public function testUpload(): void { $file = $this->getUploadedFileMock(); @@ -99,7 +99,7 @@ public function testUpload() $this->storage->upload($this->object, $this->mapping); } - public function testRemove() + public function testRemove(): void { $this->filesystem ->expects($this->once()) @@ -114,7 +114,7 @@ public function testRemove() $this->storage->remove($this->object, $this->mapping); } - public function testRemoveOnNonExistentFile() + public function testRemoveOnNonExistentFile(): void { $this->filesystem ->expects($this->once()) @@ -133,7 +133,7 @@ public function testRemoveOnNonExistentFile() /** * @dataProvider pathProvider */ - public function testResolvePath($uploadDir, $expectedPath, $relative) + public function testResolvePath(?string $uploadDir, string $expectedPath, bool $relative): void { $this->mapping ->expects($this->once()) diff --git a/Tests/Storage/GaufretteStorageTest.php b/Tests/Storage/GaufretteStorageTest.php index 7607f8ac..6dd99c41 100644 --- a/Tests/Storage/GaufretteStorageTest.php +++ b/Tests/Storage/GaufretteStorageTest.php @@ -30,48 +30,18 @@ protected function getStorage() /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { $this->filesystemMap = $this->getFilesystemMapMock(); parent::setUp(); } - /** - * Tests the upload method skips a mapping which has a non - * uploadable property value. - * - * @expectedException \LogicException - * @dataProvider invalidFileProvider - * @group upload - */ - public function testUploadSkipsMappingOnInvalid($file) - { - $this->mapping - ->expects($this->once()) - ->method('getFile') - ->will($this->returnValue($file)); - - $this->mapping - ->expects($this->never()) - ->method('hasNamer'); - - $this->mapping - ->expects($this->never()) - ->method('getNamer'); - - $this->mapping - ->expects($this->never()) - ->method('getFileName'); - - $this->storage->upload($this->object, $this->mapping); - } - /** * Test the remove method skips trying to remove a file whose file name * property value returns null. */ - public function testRemoveSkipsNullFileNameProperty() + public function testRemoveSkipsNullFileNameProperty(): void { $this->mapping ->expects($this->once()) @@ -90,7 +60,7 @@ public function testRemoveSkipsNullFileNameProperty() * * @dataProvider pathProvider */ - public function testResolvePath($protocol, $filesystemKey, $uploadDir, $expectedPath, $relative) + public function testResolvePath($protocol, $filesystemKey, $uploadDir, $expectedPath, $relative): void { $this->mapping ->method('getUploadDestination') @@ -118,7 +88,7 @@ public function testResolvePath($protocol, $filesystemKey, $uploadDir, $expected $this->assertEquals($expectedPath, $path); } - public function testResolveUri() + public function testResolveUri(): void { $this->mapping ->expects($this->once()) @@ -142,7 +112,7 @@ public function testResolveUri() $this->assertEquals('/uploads/file.txt', $path); } - public function testResolveUriFileNull() + public function testResolveUriFileNull(): void { $this->mapping ->expects($this->once()) @@ -176,7 +146,7 @@ public function pathProvider() /** * Test the remove method does delete file from gaufrette filesystem. */ - public function testThatRemoveMethodDoesDeleteFile() + public function testThatRemoveMethodDoesDeleteFile(): void { $this->mapping ->expects($this->any()) @@ -206,7 +176,7 @@ public function testThatRemoveMethodDoesDeleteFile() /** * Test that FileNotFound exception is catched. */ - public function testRemoveNotFoundFile() + public function testRemoveNotFoundFile(): void { $this->mapping ->expects($this->any()) @@ -234,7 +204,7 @@ public function testRemoveNotFoundFile() $this->storage->remove($this->object, $this->mapping); } - public function testUploadSetsMetadataWhenUsingMetadataSupporterAdapter() + public function testUploadSetsMetadataWhenUsingMetadataSupporterAdapter(): void { $filesystem = $this->getFilesystemMock(); $file = $this->getUploadedFileMock(); @@ -289,7 +259,7 @@ public function testUploadSetsMetadataWhenUsingMetadataSupporterAdapter() $this->storage->upload($this->object, $this->mapping); } - public function testUploadDoesNotSetMetadataWhenUsingNonMetadataSupporterAdapter() + public function testUploadDoesNotSetMetadataWhenUsingNonMetadataSupporterAdapter(): void { $adapter = $this->createMock('\Gaufrette\Adapter'); $filesystem = $this->getFilesystemMock(); diff --git a/Tests/Storage/StorageTestCase.php b/Tests/Storage/StorageTestCase.php index 12016a75..93585013 100644 --- a/Tests/Storage/StorageTestCase.php +++ b/Tests/Storage/StorageTestCase.php @@ -4,7 +4,6 @@ use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\vfsStreamDirectory; -use Symfony\Component\HttpFoundation\File\File; use Vich\UploaderBundle\Mapping\PropertyMapping; use Vich\UploaderBundle\Mapping\PropertyMappingFactory; use Vich\UploaderBundle\Storage\StorageInterface; @@ -53,7 +52,7 @@ abstract protected function getStorage(); /** * Sets up the test. */ - protected function setUp() + protected function setUp(): void { $this->factory = $this->getPropertyMappingFactoryMock(); $this->mapping = $this->getPropertyMappingMock(); @@ -74,20 +73,6 @@ protected function setUp() ]); } - public function invalidFileProvider() - { - $file = new File('dummy.file', false); - - return [ - // skipped because null - [null], - // skipped because not even a file - [new \DateTime()], - // skipped because not instance of UploadedFile - [$file], - ]; - } - public function emptyFilenameProvider() { return [ @@ -99,7 +84,7 @@ public function emptyFilenameProvider() /** * @dataProvider emptyFilenameProvider */ - public function testResolvePathWithEmptyFile($filename) + public function testResolvePathWithEmptyFile($filename): void { $this->mapping ->expects($this->once()) @@ -118,7 +103,7 @@ public function testResolvePathWithEmptyFile($filename) /** * @dataProvider emptyFilenameProvider */ - public function testResolveUriWithEmptyFile($filename) + public function testResolveUriWithEmptyFile($filename): void { $this->mapping ->expects($this->once()) diff --git a/Tests/Templating/Helper/UploadHelperTest.php b/Tests/Templating/Helper/UploadHelperTest.php index 472a8379..82d90dfb 100644 --- a/Tests/Templating/Helper/UploadHelperTest.php +++ b/Tests/Templating/Helper/UploadHelperTest.php @@ -16,18 +16,18 @@ class UploadHelperTest extends TestCase protected $helper; - protected function setUp() + protected function setUp(): void { $this->storage = $this->createMock('Vich\UploaderBundle\Storage\StorageInterface'); $this->helper = new UploaderHelper($this->storage); } - public function testGetName() + public function testGetName(): void { $this->assertSame('vich_uploader', $this->helper->getName()); } - public function testAssetForwardsCallsToTheStorage() + public function testAssetForwardsCallsToTheStorage(): void { $obj = new \stdClass(); diff --git a/Tests/Twig/Extension/UploaderExtensionTest.php b/Tests/Twig/Extension/UploaderExtensionTest.php index 3f75935b..139fced5 100644 --- a/Tests/Twig/Extension/UploaderExtensionTest.php +++ b/Tests/Twig/Extension/UploaderExtensionTest.php @@ -16,18 +16,13 @@ class UploaderExtensionTest extends TestCase protected $extension; - protected function setUp() + protected function setUp(): void { $this->helper = $this->getMockBuilder('Vich\UploaderBundle\Templating\Helper\UploaderHelper')->disableOriginalConstructor()->getMock(); $this->extension = new UploaderExtension($this->helper); } - public function testGetName() - { - $this->assertSame('vich_uploader', $this->extension->getName()); - } - - public function testAssetIsRegistered() + public function testAssetIsRegistered(): void { $functions = $this->extension->getFunctions(); @@ -35,7 +30,7 @@ public function testAssetIsRegistered() $this->assertSame('vich_uploader_asset', $functions[0]->getName()); } - public function testAssetForwardsCallsToTheHelper() + public function testAssetForwardsCallsToTheHelper(): void { $obj = new \stdClass(); diff --git a/Tests/Util/FilenameUtilsTest.php b/Tests/Util/FilenameUtilsTest.php index 2256540a..e65c9f21 100644 --- a/Tests/Util/FilenameUtilsTest.php +++ b/Tests/Util/FilenameUtilsTest.php @@ -10,7 +10,7 @@ class FilenameUtilsTest extends TestCase /** * @dataProvider spitNameByExtensionProvider */ - public function testSpitNameByExtension($filename, $basename, $extension) + public function testSpitNameByExtension($filename, $basename, $extension): void { $this->assertSame([$basename, $extension], FilenameUtils::spitNameByExtension($filename)); } diff --git a/Twig/Extension/UploaderExtension.php b/Twig/Extension/UploaderExtension.php index 1f268089..514f01a9 100644 --- a/Twig/Extension/UploaderExtension.php +++ b/Twig/Extension/UploaderExtension.php @@ -16,32 +16,12 @@ class UploaderExtension extends \Twig_Extension */ private $helper; - /** - * Constructs a new instance of UploaderExtension. - * - * @param UploaderHelper $helper - */ public function __construct(UploaderHelper $helper) { $this->helper = $helper; } - /** - * Returns the canonical name of this helper. - * - * @return string The canonical name - */ - public function getName() - { - return 'vich_uploader'; - } - - /** - * Returns a list of twig functions. - * - * @return array An array - */ - public function getFunctions() + public function getFunctions(): array { return [ new \Twig_SimpleFunction('vich_uploader_asset', [$this, 'asset']), @@ -49,8 +29,7 @@ public function getFunctions() } /** - * Gets the public path for the file associated with the uploadable - * object. + * Gets the public path for the file associated with the uploadable object. * * @param object $obj The object * @param string $fieldName The field name @@ -58,7 +37,7 @@ public function getFunctions() * * @return string|null The public path or null if file not stored */ - public function asset($obj, $fieldName, $className = null) + public function asset($obj, string $fieldName, string $className = null): ?string { return $this->helper->asset($obj, $fieldName, $className); } diff --git a/Util/ClassUtils.php b/Util/ClassUtils.php index 98ed7610..3b5d0ff8 100644 --- a/Util/ClassUtils.php +++ b/Util/ClassUtils.php @@ -20,7 +20,7 @@ private function __construct() * * @return string The FQCN of the given object */ - public static function getClass($object) + public static function getClass($object): string { if (class_exists(DoctrineClassUtils::class)) { return DoctrineClassUtils::getClass($object); diff --git a/Util/FilenameUtils.php b/Util/FilenameUtils.php index 744ee2d3..384fddfa 100644 --- a/Util/FilenameUtils.php +++ b/Util/FilenameUtils.php @@ -18,7 +18,7 @@ private function __construct() * * @return array An array of basename and extension */ - public static function spitNameByExtension($filename) + public static function spitNameByExtension(string $filename): array { if (false === $pos = strrpos($filename, '.')) { return [$filename, '']; diff --git a/Util/Transliterator.php b/Util/Transliterator.php index 1997a480..dea8fe82 100644 --- a/Util/Transliterator.php +++ b/Util/Transliterator.php @@ -21,9 +21,9 @@ private function __construct() * * @return string */ - public static function transliterate($string, $separator = '-') + public static function transliterate(string $string, string $separator = '-'): string { - list($filename, $extension) = FilenameUtils::spitNameByExtension($string); + [$filename, $extension] = FilenameUtils::spitNameByExtension($string); $transliterated = BehatTransliterator::transliterate($filename, $separator); if ('' !== $extension) { diff --git a/VichUploaderBundle.php b/VichUploaderBundle.php index f8b337b5..1323211a 100644 --- a/VichUploaderBundle.php +++ b/VichUploaderBundle.php @@ -17,7 +17,7 @@ class VichUploaderBundle extends Bundle /** * {@inheritdoc} */ - public function build(ContainerBuilder $container) + public function build(ContainerBuilder $container): void { parent::build($container); diff --git a/composer.json b/composer.json index 797533d0..cab4ed77 100644 --- a/composer.json +++ b/composer.json @@ -16,44 +16,45 @@ ], "extra": { "branch-alias": { - "dev-master": "1.7.x-dev" + "dev-master": "1.8.x-dev" } }, "require": { - "php": "^7.0", + "php": "^7.1", "behat/transliterator": "^1.2", - "doctrine/annotations": "^1.3", + "doctrine/annotations": "^1.6", "jms/metadata": "^1.6", - "symfony/config": "^2.8|^3.3|^4.0", - "symfony/dependency-injection": "^2.8|^3.3|^4.0", - "symfony/event-dispatcher": "^2.8.8|^3.3|^4.0", - "symfony/form": "^2.8.8|^3.3|^4.0", - "symfony/http-foundation": "^2.8.8|^3.3|^4.0", - "symfony/http-kernel": "^2.8.8|^3.3|^4.0", - "symfony/property-access": "^2.8|^3.3|^4.0", - "symfony/templating": "^2.8|^3.3|^4.0" + "symfony/config": "^3.4|^4.0", + "symfony/dependency-injection": "^3.4|^4.0", + "symfony/event-dispatcher": "^3.4|^4.0", + "symfony/form": "^3.4|^4.0", + "symfony/http-foundation": "^3.4|^4.0", + "symfony/http-kernel": "^3.4|^4.0", + "symfony/property-access": "^3.4|^4.0", + "symfony/templating": "^3.4|^4.0" }, "require-dev": { "ext-sqlite3": "*", "doctrine/doctrine-bundle": "^1.8", - "doctrine/mongodb-odm": "^1.1", + "doctrine/mongodb-odm": "^1.2", "doctrine/orm": "^2.5", "knplabs/knp-gaufrette-bundle": "^0.4@dev", "matthiasnoback/symfony-dependency-injection-test": "^2.3", "mikey179/vfsStream": "^1.6", - "oneup/flysystem-bundle": "^2.0|^3.0", - "phpunit/phpunit": "^6.0", - "symfony/browser-kit": "^2.8|^3.3|^4.0", - "symfony/css-selector": "^2.8|^3.3|^4.0", - "symfony/dom-crawler": "^2.8|^3.3|^4.0", - "symfony/framework-bundle": "^2.8.18|^3.3|^4.0", + "oneup/flysystem-bundle": "^3.0", + "phpunit/phpunit": "^6.5", + "symfony/browser-kit": "^3.4|^4.0", + "symfony/css-selector": "^3.4|^4.0", + "symfony/doctrine-bridge": "^3.4|^4.0", + "symfony/dom-crawler": "^3.4|^4.0", + "symfony/framework-bundle": "^3.4|^4.0", "symfony/phpunit-bridge": "^3.3", - "symfony/security-csrf": "^2.8|^3.3|^4.0", - "symfony/twig-bridge": "^2.8.10|^3.3|^4.0", - "symfony/twig-bundle": "^2.8|^3.3|^4.0", - "symfony/validator": "^2.8|^3.3|^4.0", - "symfony/var-dumper": "^2.8|^3.3|^4.0", - "symfony/yaml": "^2.8|^3.3|^4.0" + "symfony/security-csrf": "^3.4|^4.0", + "symfony/twig-bridge": "^3.4|^4.0", + "symfony/twig-bundle": "^3.4|^4.0", + "symfony/validator": "^3.4|^4.0", + "symfony/var-dumper": "^3.4|^4.0", + "symfony/yaml": "^3.4|^4.0" }, "suggest": { "doctrine/doctrine-bundle": "For integration with Doctrine",