Commit 9cb351de authored by Raidelet Nicolas's avatar Raidelet Nicolas Committed by Guillaume Perréal
Browse files

Fix quelques bugs potentiels et CS (PhpStorm inspection)

parent c8336d8f
......@@ -17,6 +17,9 @@ use Symfony\Component\Console\Output\OutputInterface;
*/
class CheckCommand extends ContainerAwareCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
......@@ -24,6 +27,9 @@ class CheckCommand extends ContainerAwareCommand
->setDescription("Vérifie l'intégrité des fichiers.");
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/* @var $manager FileManagerInterface */
......
......@@ -18,6 +18,9 @@ use Symfony\Component\Console\Output\OutputInterface;
*/
class CollectGarbageCommand extends ContainerAwareCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
......@@ -26,6 +29,9 @@ class CollectGarbageCommand extends ContainerAwareCommand
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Affiche ce qui devrait être fait, sans le faire');
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/* @var $manager FileManagerInterface */
......
......@@ -19,6 +19,9 @@ use Symfony\Component\Console\Output\OutputInterface;
*/
class CreateCommand extends ContainerAwareCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
......@@ -30,12 +33,15 @@ class CreateCommand extends ContainerAwareCommand
->addArgument('path', InputArgument::REQUIRED, "Nom du fichier à ajouter, - pour lire l'entrée standard");
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/* @var $em EntityManager */
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$id = $written = null;
$written = null;
$em->transactional(
function () use ($input, $output, $em, &$file, &$written) {
......@@ -73,7 +79,7 @@ class CreateCommand extends ContainerAwareCommand
$manager->completed($file);
if ($mimeType) {
$file->setMimeType($mimetype);
$file->setMimeType($mimeType);
}
if ($metadata) {
$file->setMetadata(array_merge($file->getMetadata(), $metadata));
......
......@@ -18,6 +18,9 @@ use Symfony\Component\Console\Output\OutputInterface;
*/
class ReadCommand extends ContainerAwareCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
......@@ -29,6 +32,9 @@ class ReadCommand extends ContainerAwareCommand
->addArgument('filepath', InputArgument::OPTIONAL, 'Chemin du fichier dans lequel écrire.');
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/* @var $manager FileManagerInterface */
......
......@@ -130,15 +130,17 @@ class UploadController extends Controller
/**
* @Route("/{id}/content", name="file_upload_put_content")
* @Method("PUT")
* @param Request $request
* @param Request $request
* @param UploadedFile $file
*
* @return JsonResponse|Response
*/
public function putContentAction(Request $request, UploadedFile $file)
{
try {
$this->validateCsrfToken($request);
list($offset, $maxlen, $complete) = $this->handleRangeHeader($request);
[$offset, $maxlen, $complete] = $this->handleRangeHeader($request);
// Demande un filehandle plutôt que charger le contenu en mémoire
$input = $request->getContent(true);
......@@ -162,7 +164,7 @@ class UploadController extends Controller
*/
protected function handleRangeHeader(Request $request)
{
if (null === $range = $request->headers->get('Content-Range', null)) {
if (null === $range = $request->headers->get('Content-Range')) {
return [0, PHP_INT_MAX, true];
}
......@@ -171,9 +173,9 @@ class UploadController extends Controller
throw new BadRequestHttpException('Invalid Content-Range');
}
$start = intval($matches[1]);
$end = intval($matches[2]);
$total = intval($matches[3]);
$start = (int)$matches[1];
$end = (int)$matches[2];
$total = (int)$matches[3];
if ($start < 0 || $start >= $end || $end >= $total) {
throw new HttpException(Response::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE);
......@@ -215,8 +217,10 @@ class UploadController extends Controller
/**
* @Route("/{id}/content", name="file_upload_get_content")
* @Method("GET")
* @param Request $request
* @param Request $request
* @param UploadedFile $file
*
* @return UploadedFileResponse
*/
public function getContentAction(Request $request, UploadedFile $file)
{
......@@ -235,8 +239,10 @@ class UploadController extends Controller
/**
* @Route("/{id}", name="file_upload_delete")
* @Method("DELETE")
* @param Request $request
* @param Request $request
* @param UploadedFile $file
*
* @return JsonResponse
*/
public function deleteAction(Request $request, UploadedFile $file)
{
......@@ -258,7 +264,7 @@ class UploadController extends Controller
*/
protected function validateCsrfToken(Request $request)
{
$token = $this->csrfTokenManager->getToken($request->query->get('token', null));
$token = $this->csrfTokenManager->getToken($request->query->get('token'));
if (!$this->csrfTokenManager->isTokenValid($token)) {
throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid CSRF token');
}
......
......@@ -28,6 +28,10 @@ class UploadedFileController extends Controller
* @Route("/", name="files")
* @Method("GET")
* @Template()
*
* @param Request $request
*
* @return array
*/
public function indexAction(Request $request)
{
......@@ -53,6 +57,10 @@ class UploadedFileController extends Controller
* @Route("/{id}", name="files_show")
* @Method("GET")
* @Template()
*
* @param UploadedFile $uploadedFile
*
* @return array
*/
public function showAction(UploadedFile $uploadedFile)
{
......
......@@ -30,7 +30,7 @@ class Configuration implements ConfigurationInterface
->ifString()
->then(
function ($v) {
$int = intval($v);
$int = (int)$v;
if (strpos($v, 'K')) {
return 1000 * $int;
}
......
......@@ -12,6 +12,9 @@ use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
/**
* {@inheritDoc}
*/
class IrsteaFileUploadExtension extends Extension implements PrependExtensionInterface
{
/**
......
......@@ -17,6 +17,7 @@ use Irstea\FileUploadBundle\Exception\RejectedFileException;
use Irstea\FileUploadBundle\FileUploadEvents;
use Irstea\FileUploadBundle\Model\FileManagerInterface;
use Irstea\FileUploadBundle\Model\UploadedFileInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LogLevel;
use Rhumsaa\Uuid\Uuid;
use Symfony\Component\EventDispatcher\EventDispatcher;
......@@ -28,7 +29,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
*/
class UploadedFileRepository extends EntityRepository implements FileManagerInterface
{
use \Psr\Log\LoggerAwareTrait;
use LoggerAwareTrait;
/**
* @var Filesystem
......
......@@ -62,19 +62,19 @@ class UploadedFile implements UploadedFileInterface
* @ORM\Column(type="string", length=255, nullable=true)
* @var string
*/
private $mimeType = null;
private $mimeType;
/**
* @ORM\Column(type="integer", nullable=true)
* @var int
*/
private $size = null;
private $size;
/**
* @ORM\Column(type="string", length=64, nullable=true)
* @var string
*/
private $checksum = null;
private $checksum;
/**
* @ORM\Column(type="string", length=10)
......@@ -104,13 +104,13 @@ class UploadedFile implements UploadedFileInterface
* @ORM\Column(type="json_array", nullable=true)
* @var array
*/
private $metadata = null;
private $metadata;
/**
* @ORM\Column(type="string", length=256, nullable=true)
* @var string
*/
private $description = null;
private $description;
/**
* @var Filesystem
......@@ -120,7 +120,7 @@ class UploadedFile implements UploadedFileInterface
/** Contient le nom de chemin local.
* @var string
*/
private $localTempPath = null;
private $localTempPath;
/** Crée un UploadedFile.
* @internal
......@@ -170,7 +170,7 @@ class UploadedFile implements UploadedFileInterface
*/
public function getActualPath()
{
if (!isset($this->actualPath)) {
if (null === $this->actualPath) {
$this->actualPath = $this->path;
}
......@@ -266,7 +266,8 @@ class UploadedFile implements UploadedFileInterface
self::ETAT_NORMAL,
self::ETAT_ORPHELIN,
self::ETAT_REJETE,
]
],
true
)) {
throw new InvalidArgumentException(sprintf("Etat invalide: '%s'", (string)$etat));
}
......
......@@ -20,10 +20,12 @@ class RejectedFileException extends RuntimeException implements Exception
protected $uploadedFile;
/**
* @param UploadedFileInterface $file
* @param string $message
* @param int $code
* @param \Exception $previous
* @param UploadedFileInterface $uploadedFile
* @param string $message
* @param int $code
* @param \Exception $previous
*
* @internal param UploadedFileInterface $file
*/
public function __construct(UploadedFileInterface $uploadedFile, $message, $code = 0, $previous = null)
{
......
......@@ -10,6 +10,7 @@ use Irstea\FileUploadBundle\Form\DataTranformer\UploadedFileTransformer;
use Irstea\FileUploadBundle\Model\FileManagerInterface;
use Irstea\FileUploadBundle\Validation\FileMimeType;
use Irstea\FileUploadBundle\Validation\FileSize;
use Irstea\ThemeBundle\Form\AbstractJQueryWidgetTrait;
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener;
......@@ -36,7 +37,7 @@ use Symfony\Component\Validator\Constraints\NotBlank;
*/
class FileUploadType extends AbstractType
{
use \Irstea\ThemeBundle\Form\AbstractJQueryWidgetTrait;
use AbstractJQueryWidgetTrait;
/** Routeur.
* @var Router
......@@ -125,7 +126,10 @@ class FileUploadType extends AbstractType
/** Ajoute des contraintes de validation correspondants aux paramètres du champ.
* @todo Implémenter min_file_size, max_file_size && accept_file_types.
*
* @param OptionsResolver $options
* @param mixed $constraints
*
* @return array|mixed
*/
protected function addConstraints(OptionsResolver $options, $constraints)
{
......
......@@ -162,7 +162,7 @@ class UploadedFileResponse extends Response
$range = $request->headers->get('Range');
$fileSize = $this->file->getSize();
list($start, $end) = explode('-', substr($range, 6), 2) + [0];
[$start, $end] = explode('-', substr($range, 6), 2) + [0];
$end = ('' === $end) ? $fileSize - 1 : (int)$end;
......
......@@ -8,6 +8,9 @@ namespace Irstea\FileUploadBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* {@inheritDoc}
*/
class IrsteaFileUploadBundle extends Bundle
{
}
......@@ -34,6 +34,7 @@ class CreationDataListener
}
/**
* @param UploadedFileInterface $file
* @param LifecycleEventArgs $event
*/
public function prePersist(UploadedFileInterface $file, LifecycleEventArgs $event)
......
......@@ -18,7 +18,7 @@
} else {
unit = '';
}
'UPLOADING !'
return Translator.trans('file_size(%size%,%unit%)', {size: size, unit: unit}, 'file_upload');
},
formatBitrate = function (rate) {
......
......@@ -16,8 +16,8 @@ interface FileUrlGeneratorInterface
/**
* Génère une URL sécurisée pour un fichier.
*
* @param string $idFile identifiant du fichier pour lequel générer l'URL
* @param string|bool $referenceType type d'URL à générer
* @param string $idFile identifiant du fichier pour lequel générer l'URL
* @param bool|int|string $referenceType type d'URL à générer
*
* @return string L'url générée
*/
......
......@@ -37,7 +37,7 @@ class UploadedFileTest extends PHPUnit_Framework_TestCase
*/
public function testIsSafePath($isSafe, $path)
{
$this->assertEquals($isSafe, UploadedFile::isSafePath($path));
static::assertEquals($isSafe, UploadedFile::isSafePath($path));
}
public function getIsSafePathvalues()
......@@ -59,6 +59,6 @@ class UploadedFileTest extends PHPUnit_Framework_TestCase
$this->file->moveTo('machin/chose/');
$this->assertEquals('machin/chose/muche', $this->file->getPath());
static::assertEquals('machin/chose/muche', $this->file->getPath());
}
}
......@@ -10,6 +10,7 @@ use Irstea\FileUploadBundle\Form\DataTranformer\UploadedFileTransformer;
use Irstea\FileUploadBundle\Model\UploadedFileInterface;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit_Framework_TestCase;
use Irstea\FileUploadBundle\Model\FileManagerInterface;
/**
* Generated by PHPUnit_SkeletonGenerator on 2015-01-27 at 11:36:06.
......@@ -32,7 +33,7 @@ class UploadedFileTransformerTest extends PHPUnit_Framework_TestCase
*/
protected function setUp()
{
$this->fileManager = $this->getMock('\Irstea\FileUploadBundle\Model\FileManagerInterface');
$this->fileManager = $this->getMock(FileManagerInterface::class);
$this->transformer = new UploadedFileTransformer($this->fileManager, true);
}
......@@ -46,7 +47,7 @@ class UploadedFileTransformerTest extends PHPUnit_Framework_TestCase
*/
public function testEmptyValues($expected, $method, $value)
{
$this->assertEquals($expected, $this->transformer->$method($value));
static::assertEquals($expected, $this->transformer->$method($value));
}
public function getEmptyValues()
......@@ -72,11 +73,11 @@ class UploadedFileTransformerTest extends PHPUnit_Framework_TestCase
public function testTransformFile()
{
$file = $this->getMock('\Irstea\FileUploadBundle\Model\UploadedFileInterface');
$file = $this->getMock(UploadedFileInterface::class);
$file->expects($this->once())->method('toArray')->willReturn(['bla']);
$file->expects(static::once())->method('toArray')->willReturn(['bla']);
$this->assertEquals(['bla'], $this->transformer->transformFile($file));
static::assertEquals(['bla'], $this->transformer->transformFile($file));
}
/**
......@@ -89,38 +90,38 @@ class UploadedFileTransformerTest extends PHPUnit_Framework_TestCase
public function testReverseTransformFileNoId()
{
$this->assertNull($this->transformer->reverseTransformFile(['desc' => '']));
static::assertNull($this->transformer->reverseTransformFile(['desc' => '']));
}
public function testReverseTransformFile()
{
$file = $this->getMock('\Irstea\FileUploadBundle\Model\UploadedFileInterface');
$file = $this->getMock(UploadedFileInterface::class);
$this->fileManager->expects($this->once())->method('get')->with('FOO')->willReturn($file);
$file->expects($this->once())->method('isOrphelin')->willReturn(true);
$file->expects($this->once())->method('setEtat')->with(UploadedFileInterface::ETAT_NORMAL);
$file->expects($this->once())->method('setDescription')->with('truc');
$this->fileManager->expects(static::once())->method('get')->with('FOO')->willReturn($file);
$file->expects(static::once())->method('isOrphelin')->willReturn(true);
$file->expects(static::once())->method('setEtat')->with(UploadedFileInterface::ETAT_NORMAL);
$file->expects(static::once())->method('setDescription')->with('truc');
$this->assertSame($file, $this->transformer->reverseTransformFile(['id' => 'FOO', 'description' => 'truc']));
static::assertSame($file, $this->transformer->reverseTransformFile(['id' => 'FOO', 'description' => 'truc']));
}
public function testTransformEmptyEntries()
{
$file = $this->getMock('\Irstea\FileUploadBundle\Model\UploadedFileInterface');
$file->expects($this->any())->method('toArray')->willReturn(['bla']);
$file = $this->getMock(UploadedFileInterface::class);
$file->expects(static::any())->method('toArray')->willReturn(['bla']);
$result = $this->transformer->transform([$file, null, $file]);
$this->assertEquals([['bla'], ['bla']], $result);
static::assertEquals([['bla'], ['bla']], $result);
}
public function testReverseTransformEmptyEntries()
{
$file = $this->getMock('\Irstea\FileUploadBundle\Model\UploadedFileInterface');
$this->fileManager->expects($this->any())->method('get')->with('FOO')->willReturn($file);
$file = $this->getMock(UploadedFileInterface::class);
$this->fileManager->expects(static::any())->method('get')->with('FOO')->willReturn($file);
$result = $this->transformer->reverseTransform([['id' => 'FOO'], null, ['description' => 'muche'], ['id' => 'FOO']]);
$this->assertEquals([$file, $file], $result);
static::assertEquals([$file, $file], $result);
}
}
......@@ -10,6 +10,10 @@ use Doctrine\ORM\Event\LifecycleEventArgs;
use Gaufrette\Exception\FileNotFound;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit_Framework_TestCase;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\UnitOfWork;
use Irstea\FileUploadBundle\Entity\UploadedFile;
use Gaufrette\Filesystem;
/**
* Generated by PHPUnit_SkeletonGenerator on 2015-01-29 at 14:56:00.
......@@ -52,26 +56,26 @@ class UploadedFileListenerTest extends PHPUnit_Framework_TestCase
*/
protected function setUp()
{
$this->filesystem = $this->getMockBuilder('\Gaufrette\Filesystem')
$this->filesystem = $this->getMockBuilder(Filesystem::class)
->disableOriginalConstructor()
->getMock();
$this->listener = new UploadedFileListener($this->filesystem);
$this->file = $this->getMockBuilder('\Irstea\FileUploadBundle\Entity\UploadedFile')
$this->file = $this->getMockBuilder(UploadedFile::class)
->setMethods(['setFilesystem', 'getPath'])
->getMockForAbstractClass();
$this->unitOfWork = $this->getMockBuilder('\Doctrine\ORM\UnitOfWork')
$this->unitOfWork = $this->getMockBuilder(UnitOfWork::class)
->disableOriginalConstructor()
->setMethods(['getEntityChangeSet'])
->getMock();
$this->om = $this->getMockBuilder('\Doctrine\ORM\EntityManager')
$this->om = $this->getMockBuilder(EntityManager::class)
->setMethods(['getUnitOfWork'])
->disableOriginalConstructor()
->getMock();
$this->om->expects($this->any())->method('getUnitOfWork')->willReturn($this->unitOfWork);
$this->om->expects(static::any())->method('getUnitOfWork')->willReturn($this->unitOfWork);
$this->event = new LifecycleEventArgs($this->file, $this->om);
}
......@@ -82,7 +86,7 @@ class UploadedFileListenerTest extends PHPUnit_Framework_TestCase
*/
public function testPostLoad()
{
$this->file->expects($this->once())->method('setFilesystem')->with($this->identicalTo($this->filesystem));
$this->file->expects(static::once())->method('setFilesystem')->with(static::identicalTo($this->filesystem));
$this->listener->postLoad($this->file, $this->event);
}
......@@ -92,9 +96,9 @@ class UploadedFileListenerTest extends PHPUnit_Framework_TestCase
*/
public function testPostUpdateNoPathChange()
{
$this->unitOfWork->expects($this->once())->method('getEntityChangeSet')->with($this->file)->willReturn([]);
$this->unitOfWork->expects(static::once())->method('getEntityChangeSet')->with($this->file)->willReturn([]);
$this->filesystem->expects($this->never())->method('rename');
$this->filesystem->expects(static::never())->method('rename');
$this->listener->postUpdate($this->file, $this->event);
}
......@@ -104,11 +108,11 @@ class UploadedFileListenerTest extends PHPUnit_Framework_TestCase
*/
public function testPostUpdatePathChanged()
{
$this->unitOfWork->expects($this->once())->method('getEntityChangeSet')->with($this->file)->willReturn(
$this->unitOfWork->expects(static::once())->method('getEntityChangeSet')->with($this->file)->willReturn(
['path' => ['before', 'after']]
);
$this->filesystem->expects($this->once())->method('rename')->with('before', 'after');
$this->filesystem->expects(static::once())->method('rename')->with('before', 'after');
$this->listener->postUpdate($this->file, $this->event);
}
......@@ -118,10 +122,10 @@ class UploadedFileListenerTest extends PHPUnit_Framework_TestCase
*/
public function testRemove()
{
$this->file->expects($this->any())->method('getId')->willReturn('bla');
$this->file->expects($this->once())->method('getPath')->willReturn('foobar');
$this->file->expects(static::any())->method('getId')->willReturn('bla');
$this->file->expects(static::once())->method('getPath')->willReturn('foobar');
$this->filesystem->expects($this->once())->method('delete')->with('foobar');
$this->filesystem->expects(static::once())->method('delete')->with('foobar');
$this->listener->preRemove($this->file, $this->event);
$this->listener->postRemove($this->file, $this->event);
......@@ -132,10 +136,10 @@ class UploadedFileListenerTest extends PHPUnit_Framework_TestCase
*/
public function testRemoveIgnoreFileNotFound()
{
$this->file->expects($this->any())->method('getId')->willReturn('bla');
$this->file->expects($this->once())->method('getPath')->willReturn('foobar');
$this->file->expects(static::any())->method('getId')->willReturn('bla');
$this->file->expects(static::once())->method('getPath')->willReturn('foobar');
$this->filesystem->expects($this->once())->method('delete')->with('foobar')->willThrowException(new FileNotFound('foobar'));
$this->filesystem->expects(static::once())->method('delete')->with('foobar')->willThrowException(new FileNotFound('foobar'));
$this->listener->preRemove($this->file, $this->event);
$this->listener->postRemove($this->file, $this->event); <