Factory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 14.15 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 4
dl 15
loc 106
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 1
A setUser() 0 3 1
A setL10n() 0 3 1
A createCollection() 0 3 1
A getFormatter() 0 18 4
A get() 0 8 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
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Activity\Parameter;
24
25
26
use OCA\Activity\CurrentUser;
27
use OCA\Activity\Formatter\IFormatter;
28
use OCA\Activity\Formatter\BaseFormatter;
29
use OCA\Activity\Formatter\CloudIDFormatter;
30
use OCA\Activity\Formatter\FileFormatter;
31
use OCA\Activity\Formatter\UserFormatter;
32
use OCA\Activity\ViewInfoCache;
33
use OCP\Activity\IEvent;
34
use OCP\Activity\IManager;
35
use OCP\Contacts\IManager as IContactsManager;
36
use OCP\IL10N;
37
use OCP\IURLGenerator;
38
use OCP\IUserManager;
39
40
class Factory {
41
	/** @var IManager */
42
	protected $activityManager;
43
44
	/** @var IUserManager */
45
	protected $userManager;
46
47
	/** @var IContactsManager */
48
	protected $contactsManager;
49
50
	/** @var IL10N */
51
	protected $l;
52
53
	/** @var ViewInfoCache */
54
	protected $infoCache;
55
56
	/** @var string */
57
	protected $user;
58
59
	/** @var IURLGenerator */
60
	protected $urlGenerator;
61
62
	/**
63
	 * @param IManager $activityManager
64
	 * @param IUserManager $userManager
65
	 * @param IURLGenerator $urlGenerator
66
	 * @param IContactsManager $contactsManager
67
	 * @param ViewInfoCache $infoCache,
0 ignored issues
show
Documentation introduced by
There is no parameter named $infoCache,. Did you maybe mean $infoCache?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
68
	 * @param IL10N $l
69
	 * @param CurrentUser $currentUser
70
	 */
71 27 View Code Duplication
	public function __construct(IManager $activityManager,
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...
72
								IUserManager $userManager,
73
								IURLGenerator $urlGenerator,
74
								IContactsManager $contactsManager,
75
								ViewInfoCache $infoCache,
76
								IL10N $l,
77
								CurrentUser $currentUser) {
78 27
		$this->activityManager = $activityManager;
79 27
		$this->userManager = $userManager;
80 27
		$this->urlGenerator = $urlGenerator;
81 27
		$this->contactsManager = $contactsManager;
82 27
		$this->infoCache = $infoCache;
83 27
		$this->l = $l;
84 27
		$this->user = (string) $currentUser->getUID();
85 27
	}
86
87
	/**
88
	 * @param string $user
89
	 */
90 7
	public function setUser($user) {
91 7
		$this->user = (string) $user;
92 7
	}
93
94
	/**
95
	 * @param IL10N $l
96
	 */
97 5
	public function setL10n(IL10N $l) {
98 5
		$this->l = $l;
99 5
	}
100
101
	/**
102
	 * @param string $parameter
103
	 * @param IEvent $event
104
	 * @param string $formatter
105
	 * @return IParameter
106
	 */
107 6
	public function get($parameter, IEvent $event, $formatter) {
108 6
		return new Parameter(
109 6
			$parameter,
110
			$event,
111 6
			$this->getFormatter($formatter),
112
			$formatter
113
		);
114
	}
115
116
	/**
117
	 * @return Collection
118
	 */
119 1
	public function createCollection() {
120 1
		return new Collection($this->l, sha1(microtime() . mt_rand()));
121
	}
122
123
	/**
124
	 * @param string $formatter
125
	 * @return IFormatter
126
	 */
127 8
	protected function getFormatter($formatter) {
128 8
		switch ($formatter) {
129 8
			case 'file':
130
				/** @var \OCA\Activity\Formatter\FileFormatter $fileFormatter */
131 5
				$fileFormatter = \OC::$server->query(FileFormatter::class);
132 5
				$fileFormatter->setUser($this->user);
133 5
				return $fileFormatter;
134 6
			case 'username':
135
				/** @var \OCA\Activity\Formatter\UserFormatter */
136 4
				return \OC::$server->query(UserFormatter::class);
137 2
			case 'federated_cloud_id':
138
				/** @var \OCA\Activity\Formatter\CloudIDFormatter */
139 1
				return \OC::$server->query(CloudIDFormatter::class);
140
			default:
141
				/** @var \OCA\Activity\Formatter\BaseFormatter */
142 1
				return \OC::$server->query(BaseFormatter::class);
143
		}
144
	}
145
}
146