AppExtension::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Twig;
4
5
use App\Entity\Tag;
6
use Doctrine\ORM\EntityManager;
7
use Twig\TwigFunction;
8
9
class AppExtension extends \Twig_Extension
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated with message: since Twig 2.7, use "Twig\Extension\AbstractExtension" 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...
10
{
11
    /**
12
     * @var EntityManager
13
     */
14
    protected $em;
15
16 41
    public function __construct(EntityManager $entityManager)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
17
    {
18 41
        $this->em = $entityManager;
19 41
    }
20
21 23
    public function getFunctions()
22
    {
23
        return array(
24 23
            new TwigFunction('allTagsAsString', array($this, 'allTagsAsString')),
25
        );
26
    }
27
28
    /**
29
     * @return string
30
     */
31 7
    public function allTagsAsString()
32
    {
33 7
        $tagsAsString = '';
34
35 7
        foreach ($this->allTags() as $tag) {
36 7
            $tagsAsString .= sprintf('"%s",', str_replace('"', '\"', $tag->getTitle()));
37
        }
38
39 7
        return trim($tagsAsString, ',');
40
    }
41
42
    /**
43
     * @return Tag[]|array
44
     */
45 7
    protected function allTags()
46
    {
47 7
        return $this->em->getRepository(Tag::class)->findAll();
48
    }
49
50
    public function getName()
51
    {
52
        return 'app_extension';
53
    }
54
}
55