Skip to content

Commit

Permalink
Merge 4.x into 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
SonataCI committed Nov 2, 2021
2 parents 44af5c6 + b3d2d00 commit 7b84784
Show file tree
Hide file tree
Showing 21 changed files with 72 additions and 63 deletions.
10 changes: 4 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
},
"conflict": {
"aws/aws-sdk-php": "<3.0",
"doctrine/dbal": "<2.11",
"doctrine/mongodb-odm": "<2.2",
"doctrine/orm": "<2.9",
"knplabs/knp-menu-bundle": "<3.0",
Expand All @@ -77,7 +76,6 @@
},
"require-dev": {
"aws/aws-sdk-php": "^3.0",
"doctrine/dbal": "^2.11",
"doctrine/mongodb-odm": "^2.2",
"doctrine/orm": "^2.9",
"jackalope/jackalope-doctrine-dbal": "^1.1",
Expand All @@ -86,10 +84,10 @@
"matthiasnoback/symfony-config-test": "^4.2",
"matthiasnoback/symfony-dependency-injection-test": "^4.0",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^0.12.84",
"phpstan/phpstan-phpunit": "^0.12.16",
"phpstan/phpstan-strict-rules": "^0.12.10",
"phpstan/phpstan-symfony": "^0.12.21",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-strict-rules": "^1.0",
"phpstan/phpstan-symfony": "^1.0",
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.16",
"psalm/plugin-symfony": "^3.0",
Expand Down
4 changes: 3 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ includes:
- phpstan-baseline.neon

parameters:
level: max
level: 8
symfony:
console_application_loader: ./phpstan-console-application.php
paths:
- src
- tests
treatPhpDocTypesAsCertain: false
checkGenericClassInNonGenericObjectType: true
checkInternalClassCaseSensitivity: true
checkMissingIterableValueType: true
checkMissingVarTagTypehint: true
checkMissingTypehints: true
checkUninitializedProperties: true
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private function attachArguments(ContainerBuilder $container, array $config): vo
->replaceArgument(3, new Reference($provider['generator']))
->replaceArgument(4, new Reference($provider['thumbnail']));

if ($provider['resizer']) {
if (null !== $provider['resizer']) {
$definition->addMethodCall('setResizer', [new Reference($provider['resizer'])]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private function addProvidersSection(ArrayNodeDefinition $node): void
->addDefaultsIfNotSet()
->children()
->scalarNode('service')->defaultValue('sonata.media.provider.file')->end()
->scalarNode('resizer')->defaultValue(false)->end()
->scalarNode('resizer')->defaultNull()->end()
->scalarNode('filesystem')->defaultValue('sonata.media.filesystem.local')->end()
->scalarNode('cdn')->defaultValue('sonata.media.cdn.server')->end()
->scalarNode('generator')->defaultValue('sonata.media.generator.default')->end()
Expand Down
6 changes: 0 additions & 6 deletions src/Document/BaseGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@

namespace Sonata\MediaBundle\Document;

use Doctrine\Common\Collections\ArrayCollection;
use Sonata\MediaBundle\Model\Gallery;

abstract class BaseGallery extends Gallery
{
public function __construct()
{
$this->galleryItems = new ArrayCollection();
}

public function prePersist(): void
{
$this->createdAt = new \DateTime();
Expand Down
6 changes: 0 additions & 6 deletions src/Document/BaseMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@

namespace Sonata\MediaBundle\Document;

use Doctrine\Common\Collections\ArrayCollection;
use Sonata\MediaBundle\Model\Media;

abstract class BaseMedia extends Media
{
public function __construct()
{
$this->galleryItems = new ArrayCollection();
}

public function prePersist(): void
{
$this->createdAt = new \DateTime();
Expand Down
6 changes: 0 additions & 6 deletions src/Entity/BaseGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@

namespace Sonata\MediaBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Sonata\MediaBundle\Model\Gallery;

abstract class BaseGallery extends Gallery
{
public function __construct()
{
$this->galleryItems = new ArrayCollection();
}

public function prePersist(): void
{
$this->createdAt = new \DateTime();
Expand Down
6 changes: 0 additions & 6 deletions src/Entity/BaseMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@

namespace Sonata\MediaBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Sonata\MediaBundle\Model\Media;

abstract class BaseMedia extends Media
{
public function __construct()
{
$this->galleryItems = new ArrayCollection();
}

public function prePersist(): void
{
$this->createdAt = new \DateTime();
Expand Down
17 changes: 15 additions & 2 deletions src/Form/DataTransformer/ProviderDataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
use Sonata\MediaBundle\Provider\Pool;
use Symfony\Component\Form\DataTransformerInterface;

/**
* @implements DataTransformerInterface<MediaInterface, MediaInterface>
*/
final class ProviderDataTransformer implements DataTransformerInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
Expand Down Expand Up @@ -52,25 +55,35 @@ public function __construct(Pool $pool, string $class, array $options = [])
* @param mixed $value
*
* @return mixed
*
* @phpstan-param MediaInterface|null $value
* @phpstan-return MediaInterface|null
*/
public function transform($value)
{
if (null === $value) {
return new $this->class();
}

if (!$value instanceof MediaInterface) {
return null;
}

return $value;
}

/**
* @param mixed $value
*
* @return mixed
*
* @phpstan-param MediaInterface|null $value
* @phpstan-return MediaInterface|null
*/
public function reverseTransform($value)
{
if (!$value instanceof MediaInterface) {
return $value;
return null;
}

$binaryContent = $value->getBinaryContent();
Expand All @@ -79,7 +92,7 @@ public function reverseTransform($value)
if (null === $binaryContent) {
// and no media id
if (null === $value->getId() && true === $this->options['empty_on_new']) {
return;
return null;
}
if (null !== $value->getId()) {
return $value;
Expand Down
5 changes: 5 additions & 0 deletions src/Model/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ abstract class Gallery implements GalleryInterface
*/
protected Collection $galleryItems;

public function __construct()
{
$this->galleryItems = new ArrayCollection();
}

public function __toString(): string
{
return $this->getName() ?? '-';
Expand Down
6 changes: 6 additions & 0 deletions src/Model/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\MediaBundle\Model;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Imagine\Image\Box;
use Sonata\ClassificationBundle\Model\CategoryInterface;
Expand Down Expand Up @@ -81,6 +82,11 @@ abstract class Media implements MediaInterface
*/
protected ?object $category = null;

public function __construct()
{
$this->galleryItems = new ArrayCollection();
}

public function __toString(): string
{
return $this->getName() ?? 'n/a';
Expand Down
9 changes: 7 additions & 2 deletions src/Model/NoDriverGalleryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Sonata\MediaBundle\Model;

use Doctrine\DBAL\Connection;
use Sonata\MediaBundle\Exception\NoDriverException;

/**
Expand Down Expand Up @@ -68,7 +67,13 @@ public function getTableName(): string
throw new NoDriverException();
}

public function getConnection(): Connection
/**
* Do not add return typehint to this method, it forces a dependency with
* Doctrine DBAL that we do not want here. This method will probably be
* deprecated on sonata-project/doctrine-extensions because it is only for
* Doctrine ORM.
*/
public function getConnection()
{
throw new NoDriverException();
}
Expand Down
9 changes: 7 additions & 2 deletions src/Model/NoDriverMediaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Sonata\MediaBundle\Model;

use Doctrine\DBAL\Connection;
use Sonata\MediaBundle\Exception\NoDriverException;

/**
Expand Down Expand Up @@ -68,7 +67,13 @@ public function getTableName(): string
throw new NoDriverException();
}

public function getConnection(): Connection
/**
* Do not add return typehint to this method, it forces a dependency with
* Doctrine DBAL that we do not want here. This method will probably be
* deprecated on sonata-project/doctrine-extensions because it is only for
* Doctrine ORM.
*/
public function getConnection()
{
throw new NoDriverException();
}
Expand Down
16 changes: 9 additions & 7 deletions src/Provider/FileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,21 @@ public function getDownloadResponse(MediaInterface $media, string $format, strin

public function validate(ErrorElement $errorElement, MediaInterface $media): void
{
if (!$media->getBinaryContent() instanceof \SplFileInfo) {
$binaryContent = $media->getBinaryContent();

if (!$binaryContent instanceof \SplFileInfo) {
return;
}

if ($media->getBinaryContent() instanceof UploadedFile) {
$fileName = $media->getBinaryContent()->getClientOriginalName();
} elseif ($media->getBinaryContent() instanceof File) {
$fileName = $media->getBinaryContent()->getFilename();
if ($binaryContent instanceof UploadedFile) {
$fileName = $binaryContent->getClientOriginalName();
} elseif ($binaryContent instanceof File) {
$fileName = $binaryContent->getFilename();
} else {
throw new \RuntimeException(sprintf('Invalid binary content type: %s', \get_class($media->getBinaryContent())));
throw new \RuntimeException(sprintf('Invalid binary content type: %s', \get_class($binaryContent)));
}

if ($media->getBinaryContent() instanceof UploadedFile && 0 === ($media->getBinaryContent()->getSize() ?? 0)) {
if ($binaryContent instanceof UploadedFile && 0 === $binaryContent->getSize()) {
$errorElement
->with('binaryContent')
->addViolation(
Expand Down
4 changes: 2 additions & 2 deletions src/Provider/ImageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getHelperProperties(MediaInterface $media, string $format, array
}

if (null === $this->resizer) {
throw new \RuntimeException('Resizer not set on the imageg provider.');
throw new \RuntimeException('Resizer not set on the image provider.');
}

$box = $this->resizer->getBox($media, $resizerFormat);
Expand Down Expand Up @@ -143,7 +143,7 @@ public function getHelperProperties(MediaInterface $media, string $format, array
// Check if format belongs to the current media's context
if (null !== $context && 0 === strpos($providerFormat, $context)) {
if (null === $this->resizer) {
throw new \RuntimeException('Resizer not set on the imageg provider.');
throw new \RuntimeException('Resizer not set on the image provider.');
}

$width = $this->resizer->getBox($media, $settings)->getWidth();
Expand Down
6 changes: 2 additions & 4 deletions tests/App/Entity/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
class Context extends BaseContext
{
/**
* @var string|null
*
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="string")
*/
protected $id;
protected ?string $id = null;

public function getId()
public function getId(): ?string
{
return $this->id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ public function processProvider(): iterable
}
}

/**
* Here we create a class with an addResizer method that is not callable on purpose,
* we want to test that we will not call it if it can't be called. Please do not remove
* this method even if some tools report it as unused, it is intended.
*/
final class TestUncallableAddResizerMethod
{
// @phpstan-ignore-next-line
private function addResizer(): void
{
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/DataTransformer/ProviderDataTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testReverseTransformFakeValue(): void
$pool = new Pool('default');

$transformer = new ProviderDataTransformer($pool, MediaInterface::class);
static::assertSame('foo', $transformer->reverseTransform('foo'));
static::assertNull($transformer->reverseTransform(null));
}

public function testReverseTransformUnknownProvider(): void
Expand Down
5 changes: 2 additions & 3 deletions tests/Form/Type/AbstractTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Sonata\MediaBundle\Tests\Form\Type;

use Sonata\MediaBundle\Provider\Pool;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\Form\Test\TypeTestCase;

Expand All @@ -23,14 +22,14 @@
*/
abstract class AbstractTypeTest extends TypeTestCase
{
protected FormBuilder $formBuilder;

protected FormTypeInterface $formType;

protected Pool $mediaPool;

protected function setUp(): void
{
parent::setUp();

$this->mediaPool = new Pool('default');
$this->formType = $this->getTestedInstance();
}
Expand Down

0 comments on commit 7b84784

Please sign in to comment.