Conditions | 16 |
Paths | 90 |
Total Lines | 96 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
19 | public function load(ObjectManager $manager) |
||
20 | { |
||
21 | $connection = $manager->getConnection(); |
||
|
|||
22 | $platform = $connection->getDatabasePlatform()->getName(); |
||
23 | |||
24 | switch ($platform) { |
||
25 | case 'postgresql': |
||
26 | $connection->exec('ALTER SEQUENCE app_form_id_seq RESTART WITH 1'); |
||
27 | break; |
||
28 | } |
||
29 | |||
30 | $env = $this->container->get('kernel')->getEnvironment(); |
||
31 | |||
32 | // @todo create mock server instead of skipping fixture |
||
33 | if ('test' === $env) { |
||
34 | return; |
||
35 | } |
||
36 | |||
37 | $configService = $this->container->get('ds_config.service.config'); |
||
38 | $service = $this->container->get('ds_api.api')->get('formio.authentication'); |
||
39 | $user = new FormioUser; |
||
40 | $user |
||
41 | ->setEmail($configService->get('ds_api.user.username')) |
||
42 | ->setPassword($configService->get('ds_api.user.password')); |
||
43 | $token = $service->login($user); |
||
44 | $service = $this->container->get('ds_api.api')->get('formio.form'); |
||
45 | $service->setHeader('x-jwt-token', $token); |
||
46 | $forms = $service->getList(); |
||
47 | |||
48 | foreach ($forms as $form) { |
||
49 | if (in_array($form->getName(), ['user', 'admin', 'userLogin', 'userRegister'])) { |
||
50 | // Skip base formio forms. |
||
51 | continue; |
||
52 | } |
||
53 | |||
54 | try { |
||
55 | $service->delete($form->getPath()); |
||
56 | } catch (ValidationException $exception) { |
||
57 | // @todo this is so first time fixtures dont cause an error, handle "Invalid alias" better |
||
58 | } |
||
59 | } |
||
60 | |||
61 | $api = $this->container->get('ds_api.api')->get('formio.role'); |
||
62 | $api->setHeader('x-jwt-token', $token); |
||
63 | $roles = $api->getList(); |
||
64 | $objects = $this->parse($this->getResource()); |
||
65 | |||
66 | foreach ($objects as $object) { |
||
67 | $form = new Form; |
||
68 | $form |
||
69 | ->setUuid($object->uuid) |
||
70 | ->setOwner($object->owner) |
||
71 | ->setOwnerUuid($object->owner_uuid) |
||
72 | ->setType($object->type) |
||
73 | ->setTenant($object->tenant); |
||
74 | |||
75 | switch ($object->type) { |
||
76 | case Form::TYPE_FORMIO: |
||
77 | $config = $object->config; |
||
78 | |||
79 | if (property_exists($config, 'components')) { |
||
80 | if (is_string($config->components)) { |
||
81 | $config->components = json_decode(file_get_contents(dirname(str_replace('{env}', $env, $this->getResource())).'/'.$config->components)); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | if (property_exists($config, 'submissionAccess')) { |
||
86 | if (is_string($config->submissionAccess)) { |
||
87 | $config->submissionAccess = json_decode(file_get_contents(dirname(str_replace('{env}', $env, $this->getResource())).'/'.$config->submissionAccess)); |
||
88 | $submissionAccess = []; |
||
89 | |||
90 | foreach ($config->submissionAccess as $access) { |
||
91 | foreach ($access->roles as $key => $value) { |
||
92 | foreach ($roles as $role) { |
||
93 | if ($role->getMachineName() === $value) { |
||
94 | $access->roles[$key] = $role->getId(); |
||
95 | break; |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | |||
100 | $submissionAccess[] = $access; |
||
101 | } |
||
102 | |||
103 | $config->submissionAccess = $submissionAccess; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | $form->setConfig($config); |
||
108 | break; |
||
109 | } |
||
110 | |||
111 | $manager->persist($form); |
||
112 | $manager->flush(); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: