Completed
Pull Request — master (#2)
by Vojtěch
10:05
created

ForgotPasswordControl   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 82
Duplicated Lines 29.27 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 24
loc 82
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createComponentForm() 24 24 1
A render() 0 4 1
A processForm() 0 12 2

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
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\ForgotPassword\Control\ForgotPassword;
6
7
use Nette;
8
use SixtyEightPublishers;
9
10
/**
11
 * @method void onSend(string $email, ?SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $passwordRequest)
12
 * @method void onError(SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestCreationException $e, string $email)
13
 * @method void onFormCreation(Nette\Application\UI\Form $form)
14
 */
15
class ForgotPasswordControl extends SixtyEightPublishers\SmartNetteComponent\UI\Control implements SixtyEightPublishers\User\Common\Translator\ITranslatorAware
16
{
17
	use SixtyEightPublishers\User\Common\Translator\TTranslatorAware;
18
19
	/** @var \SixtyEightPublishers\User\ForgotPassword\PasswordRequest\IPasswordRequestSender  */
20
	private $passwordRequestSender;
21
	
22
	/** @var callable[] */
23
	public $onSend = [];
24
25
	/** @var callable[] */
26
	public $onError = [];
27
28
	/** @var callable[] */
29
	public $onFormCreation = [];
30
31
	/**
32
	 * @param \SixtyEightPublishers\User\ForgotPassword\PasswordRequest\IPasswordRequestSender $passwordRequestSender
33
	 */
34
	public function __construct(SixtyEightPublishers\User\ForgotPassword\PasswordRequest\IPasswordRequestSender $passwordRequestSender)
35
	{
36
		parent::__construct();
37
38
		$this->passwordRequestSender = $passwordRequestSender;
39
	}
40
	
41
	/**
42
	 * @return \Nette\Application\UI\Form
43
	 */
44 View Code Duplication
	protected function createComponentForm(): Nette\Application\UI\Form
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...
45
	{
46
		$form = new Nette\Application\UI\Form();
47
48
		$form->setTranslator(SixtyEightPublishers\User\Common\Translator\PrefixedTranslator::createFromClassName(
49
			$this->getTranslator(),
50
			static::class
51
		));
52
53
		$form->addText('email', 'email.field')
54
			->setRequired('email.required')
0 ignored issues
show
Documentation introduced by
'email.required' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
			->addRule($form::EMAIL, 'email.rule')
56
			->setAttribute('autocomplete', 'username');
0 ignored issues
show
Documentation introduced by
'username' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
58
		$form->addProtection('protection.rule');
59
60
		$form->onSuccess[] = [ $this, 'processForm' ];
61
62
		$this->onFormCreation($form);
63
64
		$form->addSubmit('send', 'send.field');
65
66
		return $form;
67
	}
68
69
	/**
70
	 * @return void
71
	 */
72
	public function render(): void
73
	{
74
		$this->doRender();
75
	}
76
77
	/**
78
	 * @internal
79
	 *
80
	 * @param \Nette\Application\UI\Form $form
81
	 *
82
	 * @return void
83
	 */
84
	public function processForm(Nette\Application\UI\Form $form): void
85
	{
86
		$email = $form->values->email;
87
88
		try {
89
			$request = $this->passwordRequestSender->send($email);
90
91
			$this->onSend($email, $request);
92
		} catch (SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestCreationException $e) {
93
			$this->onError($e, $email);
94
		}
95
	}
96
}
97