These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Sonata Project package. |
||
7 | * |
||
8 | * (c) Thomas Rabaix <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Sonata\NotificationBundle\Command; |
||
15 | |||
16 | use Sonata\NotificationBundle\Backend\QueueDispatcherInterface; |
||
17 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
||
18 | use Symfony\Component\Console\Input\InputInterface; |
||
19 | use Symfony\Component\Console\Output\OutputInterface; |
||
20 | |||
21 | class CleanupCommand extends ContainerAwareCommand |
||
22 | { |
||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | public function configure(): void |
||
27 | { |
||
28 | $this->setName('sonata:notification:cleanup'); |
||
29 | $this->setDescription('Clean up backend message'); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | public function execute(InputInterface $input, OutputInterface $output): void |
||
36 | { |
||
37 | $output->write('<info>Starting ... </info>'); |
||
38 | |||
39 | $this->getBackend()->cleanup(); |
||
40 | |||
41 | $output->writeln('done!'); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @return BackendInterface |
||
46 | */ |
||
47 | private function getBackend() |
||
48 | { |
||
49 | $backend = $this->getContainer()->get('sonata.notification.backend'); |
||
50 | |||
51 | if ($backend instanceof QueueDispatcherInterface) { |
||
52 | return $backend->getBackend(null); |
||
0 ignored issues
–
show
|
|||
53 | } |
||
54 | |||
55 | return $backend; |
||
56 | } |
||
57 | } |
||
58 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.