Completed
Push — master ( 7d6b25...de2ef0 )
by Thomas
14:16
created

ListUserGroups   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 25.58 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 11
loc 43
rs 10
c 0
b 0
f 0
wmc 4
lcom 2
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 13 1
A execute() 11 11 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
 * @author Phil Davis <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Core\Command\User;
23
24
use OC\Core\Command\Base;
25
use OCP\IUserManager;
26
use OCP\IGroupManager;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Output\OutputInterface;
29
use Symfony\Component\Console\Input\InputArgument;
30
31
class ListUserGroups extends Base {
32
	/** @var \OCP\IUserManager */
33
	protected $userManager;
34
35
	/** @var \OCP\IGroupManager */
36
	protected $groupManager;
37
38
	/**
39
	 * @param IUserManager $userManager
40
	 * @param IGroupManager $groupManager
41
	 */
42
	public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
43
		parent::__construct();
44
		$this->userManager = $userManager;
45
		$this->groupManager = $groupManager;
46
	}
47
48
	protected function configure() {
49
		parent::configure();
50
51
		$this
52
			->setName('user:list-groups')
53
			->setDescription('list groups for a user')
54
			->addArgument(
55
				'uid',
56
				InputArgument::REQUIRED,
57
				'User ID'
58
			)
59
		;
60
	}
61
62 View Code Duplication
	protected function execute(InputInterface $input, OutputInterface $output) {
63
		$uid = $input->getArgument('uid');
64
		if (!$this->userManager->userExists($uid)) {
65
			$output->writeln("<error>User $uid does not exist.</error>");
66
			return 1;
67
		}
68
69
		$user = $this->userManager->get($uid);
70
		$groupNames = $this->groupManager->getUserGroupIds($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userManager->get($uid) on line 69 can be null; however, OCP\IGroupManager::getUserGroupIds() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
71
		parent::writeArrayInOutputFormat($input, $output, $groupNames);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (writeArrayInOutputFormat() instead of execute()). Are you sure this is correct? If so, you might want to change this to $this->writeArrayInOutputFormat().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
72
	}
73
}
74