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

EntityForm   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 14.89 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 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
 * EntityForm.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           26.05.13
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\Application;
23
use Nette\ComponentModel;
24
25
class EntityForm extends Application\UI\Form
26
{
27
	/**
28
	 * Implement entity handling in form
29
	 */
30
	use TEntityContainer;
31
32
	/**
33
	 * Implement form methods
34
	 */
35
	use TForm;
36
37
	/**
38
	 * @var ORM\EntityManager
39
	 */
40
	private $entityManager;
41
42
	/**
43
	 * @param ORM\EntityManager $entityManager
44
	 * @param ComponentModel\IContainer|NULL $parent
45
	 * @param string|NULL $name
46
	 */
47
	public function __construct(
48
		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...
49
		ComponentModel\IContainer $parent = NULL,
50
		string $name = NULL
51
	) {
52
		parent::__construct($parent, $name);
53
54
		$this->entityManager = $entityManager;
55
	}
56
57
	/**
58
	 * Adds naming container to the form
59
	 *
60
	 * @param string $name
61
	 *
62
	 * @return Container
63
	 */
64 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...
65
	{
66
		$control = new Container($this->entityManager);
67
		$control->setCurrentGroup($this->currentGroup);
68
69
		return $this[$name] = $control;
70
	}
71
}
72