Completed
Push — master ( 48290c...e43ea6 )
by Adam
07:23
created

Container   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 18.92 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 2
dl 7
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addContainer() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * TEntityContainer.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:Forms!
9
 * @subpackage     Forms
10
 * @since          1.0.0
11
 *
12
 * @date           10.01.16
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Forms\Forms;
18
19
use Doctrine\ORM;
20
21
use Nette;
22
use Nette\Forms;
23
24
class Container extends Forms\Container
25
{
26
	/**
27
	 * Implement entity handling in form
28
	 */
29
	use TEntityContainer;
30
31
	/**
32
	 * @var ORM\EntityManager
33
	 */
34
	private $entityManager;
35
36
	/**
37
	 * @param ORM\EntityManager $entityManager
38
	 */
39
	public function __construct(ORM\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...
40
	{
41
		parent::__construct();
42
43
		$this->entityManager = $entityManager;
44
	}
45
46
	/**
47
	 * Adds naming container to the form
48
	 *
49
	 * @param string $name
50
	 *
51
	 * @return Container
52
	 */
53 View Code Duplication
	public function addContainer($name) : Container
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
54
	{
55
		$control = new self($this->entityManager);
56
		$control->setCurrentGroup($this->currentGroup);
57
58
		return $this[$name] = $control;
59
	}
60
}
61