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

ListUserGroups::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
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