Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

FolderController::deleteAction()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 0
cts 38
cp 0
rs 9.216
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 12
1
<?php
2
3
namespace Kunstmaan\MediaBundle\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
7
use Kunstmaan\MediaBundle\AdminList\MediaAdminListConfigurator;
8
use Kunstmaan\MediaBundle\Entity\Folder;
9
use Kunstmaan\MediaBundle\Form\FolderType;
10
use Kunstmaan\MediaBundle\Helper\MediaManager;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
12
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
13
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
14
use Symfony\Component\HttpFoundation\JsonResponse;
15
use Symfony\Component\HttpFoundation\RedirectResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\Routing\Annotation\Route;
19
20
/**
21
 * FolderController.
22
 */
23
class FolderController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
24
{
25
    /**
26
     * @param int $folderId The folder id
27
     *
28
     * @Route("/{folderId}", requirements={"folderId" = "\d+"}, name="KunstmaanMediaBundle_folder_show")
29
     * @Template("@KunstmaanMedia/Folder/show.html.twig")
30
     *
31
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be RedirectResponse|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
32
     */
33
    public function showAction(Request $request, $folderId)
34
    {
35
        /** @var EntityManager $em */
36
        $em = $this->getDoctrine()->getManager();
37
        $session = $request->getSession();
38
39
        // Check when user switches between thumb -and list view
40
        $viewMode = $request->query->get('viewMode');
41
        if ($viewMode && $viewMode == 'list-view') {
42
            $session->set('media-list-view', true);
43
        } elseif ($viewMode && $viewMode == 'thumb-view') {
44
            $session->remove('media-list-view');
45
        }
46
47
        /* @var MediaManager $mediaManager */
48
        $mediaManager = $this->get('kunstmaan_media.media_manager');
49
50
        /* @var Folder $folder */
51
        $folder = $em->getRepository(Folder::class)->getFolder($folderId);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getFolder() does only exist in the following implementations of said interface: Kunstmaan\MediaBundle\Repository\FolderRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
52
53
        $adminListConfigurator = new MediaAdminListConfigurator($em, $mediaManager, $folder, $request);
54
        $adminList = $this->get('kunstmaan_adminlist.factory')->createList($adminListConfigurator);
55
        $adminList->bindRequest($request);
56
57
        $sub = new Folder();
58
        $sub->setParent($folder);
59
        $subForm = $this->createForm(FolderType::class, $sub, ['folder' => $sub]);
60
61
        $emptyForm = $this->createEmptyForm();
62
63
        $editForm = $this->createForm(FolderType::class, $folder, ['folder' => $folder]);
64
65
        if ($request->isMethod('POST')) {
66
            $editForm->handleRequest($request);
67
            if ($editForm->isValid()) {
68
                $em->getRepository(Folder::class)->save($folder);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method save() does only exist in the following implementations of said interface: Kunstmaan\MediaBundle\Repository\FolderRepository, Kunstmaan\MediaBundle\Repository\MediaRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
69
70
                $this->addFlash(
71
                    FlashTypes::SUCCESS,
72
                    $this->get('translator')->trans('media.folder.show.success.text', [
73
                        '%folder%' => $folder->getName(),
74
                    ])
75
                );
76
77
                return new RedirectResponse(
78
                    $this->generateUrl(
79
                        'KunstmaanMediaBundle_folder_show',
80
                        ['folderId' => $folderId]
81
                    )
82
                );
83
            }
84
        }
85
86
        return [
87
            'foldermanager' => $this->get('kunstmaan_media.folder_manager'),
88
            'mediamanager' => $this->get('kunstmaan_media.media_manager'),
89
            'subform' => $subForm->createView(),
90
            'emptyform' => $emptyForm->createView(),
91
            'editform' => $editForm->createView(),
92
            'folder' => $folder,
93
            'adminlist' => $adminList,
94
            'type' => null,
95
        ];
96
    }
97
98
    /**
99
     * @param int $folderId
100
     *
101
     * @Route("/delete/{folderId}", requirements={"folderId" = "\d+"}, name="KunstmaanMediaBundle_folder_delete")
102
     *
103
     * @return RedirectResponse
104
     */
105
    public function deleteAction(Request $request, $folderId)
106
    {
107
        /** @var EntityManager $em */
108
        $em = $this->getDoctrine()->getManager();
109
110
        /* @var Folder $folder */
111
        $folder = $em->getRepository(Folder::class)->getFolder($folderId);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getFolder() does only exist in the following implementations of said interface: Kunstmaan\MediaBundle\Repository\FolderRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
112
        $folderName = $folder->getName();
113
        $parentFolder = $folder->getParent();
114
115
        if (\is_null($parentFolder)) {
116
            $this->addFlash(
117
                FlashTypes::DANGER,
118
                $this->get('translator')->trans('media.folder.delete.failure.text', [
119
                    '%folder%' => $folderName,
120
                ])
121
            );
122
        } else {
123
            $em->getRepository(Folder::class)->delete($folder);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method delete() does only exist in the following implementations of said interface: Kunstmaan\MediaBundle\Repository\FolderRepository, Kunstmaan\MediaBundle\Repository\MediaRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
124
            $this->addFlash(
125
                FlashTypes::SUCCESS,
126
                $this->get('translator')->trans('media.folder.delete.success.text', [
127
                    '%folder%' => $folderName,
128
                ])
129
            );
130
            $folderId = $parentFolder->getId();
131
        }
132
        if (strpos($request->server->get('HTTP_REFERER', ''), 'chooser')) {
133
            $redirect = 'KunstmaanMediaBundle_chooser_show_folder';
134
        } else {
135
            $redirect = 'KunstmaanMediaBundle_folder_show';
136
        }
137
138
        $type = $this->get('request_stack')->getCurrentRequest()->get('type');
139
140
        return new RedirectResponse(
141
            $this->generateUrl($redirect,
142
                [
143
                    'folderId' => $folderId,
144
                    'type' => $type,
145
                ]
146
            )
147
        );
148
    }
149
150
    /**
151
     * @param int $folderId
152
     *
153
     * @Route("/subcreate/{folderId}", requirements={"folderId" = "\d+"}, name="KunstmaanMediaBundle_folder_sub_create", methods={"GET", "POST"})
154
     *
155
     * @return Response
156
     */
157
    public function subCreateAction(Request $request, $folderId)
158
    {
159
        /** @var EntityManager $em */
160
        $em = $this->getDoctrine()->getManager();
161
162
        /* @var Folder $parent */
163
        $parent = $em->getRepository(Folder::class)->getFolder($folderId);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getFolder() does only exist in the following implementations of said interface: Kunstmaan\MediaBundle\Repository\FolderRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
164
        $folder = new Folder();
165
        $folder->setParent($parent);
166
        $form = $this->createForm(FolderType::class, $folder);
167 View Code Duplication
        if ($request->isMethod('POST')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
            $form->handleRequest($request);
169
            if ($form->isSubmitted() && $form->isValid()) {
170
                $em->getRepository(Folder::class)->save($folder);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method save() does only exist in the following implementations of said interface: Kunstmaan\MediaBundle\Repository\FolderRepository, Kunstmaan\MediaBundle\Repository\MediaRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
171
                $this->addFlash(
172
                    FlashTypes::SUCCESS,
173
                    $this->get('translator')->trans('media.folder.addsub.success.text', [
174
                        '%folder%' => $folder->getName(),
175
                    ])
176
                );
177
                if (strpos($request->server->get('HTTP_REFERER', ''), 'chooser') !== false) {
178
                    $redirect = 'KunstmaanMediaBundle_chooser_show_folder';
179
                } else {
180
                    $redirect = 'KunstmaanMediaBundle_folder_show';
181
                }
182
183
                $type = $request->get('type');
184
185
                return new RedirectResponse(
186
                    $this->generateUrl($redirect,
187
                        [
188
                            'folderId' => $folder->getId(),
189
                            'type' => $type,
190
                        ]
191
                    )
192
                );
193
            }
194
        }
195
196
        $galleries = $em->getRepository(Folder::class)->getAllFolders();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getAllFolders() does only exist in the following implementations of said interface: Kunstmaan\MediaBundle\Repository\FolderRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
197
198
        return $this->render(
199
            '@KunstmaanMedia/Folder/addsub-modal.html.twig',
200
            [
201
                'subform' => $form->createView(),
202
                'galleries' => $galleries,
203
                'folder' => $folder,
204
                'parent' => $parent,
205
            ]
206
        );
207
    }
208
209
    /**
210
     * @param int $folderId
211
     *
212
     * @Route("/empty/{folderId}", requirements={"folderId" = "\d+"}, name="KunstmaanMediaBundle_folder_empty", methods={"GET", "POST"})
213
     *
214
     * @return Response
215
     */
216
    public function emptyAction(Request $request, $folderId)
217
    {
218
        /** @var EntityManager $em */
219
        $em = $this->getDoctrine()->getManager();
220
221
        /* @var Folder $folder */
222
        $folder = $em->getRepository(Folder::class)->getFolder($folderId);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getFolder() does only exist in the following implementations of said interface: Kunstmaan\MediaBundle\Repository\FolderRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
223
224
        $form = $this->createEmptyForm();
225
226
        if ($request->isMethod('POST')) {
227
            $form->handleRequest($request);
228 View Code Duplication
            if ($form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
229
                $data = $form->getData();
230
                $alsoDeleteFolders = $data['checked'];
231
232
                $em->getRepository(Folder::class)->emptyFolder($folder, $alsoDeleteFolders);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method emptyFolder() does only exist in the following implementations of said interface: Kunstmaan\MediaBundle\Repository\FolderRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
233
234
                $this->addFlash(
235
                    FlashTypes::SUCCESS,
236
                    $this->get('translator')->trans('media.folder.empty.success.text', [
237
                        '%folder%' => $folder->getName(),
238
                    ])
239
                );
240
                if (strpos($request->server->get('HTTP_REFERER', ''), 'chooser') !== false) {
241
                    $redirect = 'KunstmaanMediaBundle_chooser_show_folder';
242
                } else {
243
                    $redirect = 'KunstmaanMediaBundle_folder_show';
244
                }
245
246
                return new RedirectResponse(
247
                    $this->generateUrl($redirect,
248
                        [
249
                            'folderId' => $folder->getId(),
250
                            'folder' => $folder,
251
                        ]
252
                    )
253
                );
254
            }
255
        }
256
257
        return $this->render(
258
            '@KunstmaanMedia/Folder/empty-modal.html.twig',
259
            [
260
                'form' => $form->createView(),
261
            ]
262
        );
263
    }
264
265
    /**
266
     * @Route("/reorder", name="KunstmaanMediaBundle_folder_reorder")
267
     *
268
     * @return JsonResponse
269
     */
270
    public function reorderAction(Request $request)
271
    {
272
        $folders = [];
273
        $nodeIds = $request->get('nodes');
274
275
        $em = $this->getDoctrine()->getManager();
276
        $repository = $em->getRepository(Folder::class);
277
278
        foreach ($nodeIds as $id) {
279
            /* @var Folder $folder */
280
            $folder = $repository->find($id);
281
            $folders[] = $folder;
282
        }
283
284
        foreach ($folders as $id => $folder) {
285
            $repository->moveDown($folder, true);
286
        }
287
288
        $em->flush();
289
290
        return new JsonResponse(
291
            [
292
                'Success' => 'The node-translations for have got new weight values',
293
            ]
294
        );
295
    }
296
297
    private function createEmptyForm()
298
    {
299
        $defaultData = ['checked' => false];
300
        $form = $this->createFormBuilder($defaultData)
301
            ->add('checked', CheckboxType::class, ['required' => false, 'label' => 'media.folder.empty.modal.checkbox'])
302
            ->getForm();
303
304
        return $form;
305
    }
306
}
307