Completed
Pull Request — master (#6982)
by Blizzz
14:42
created

ShareRecipientSorter::sort()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 41
Code Lines 24

Duplication

Lines 23
Ratio 56.1 %

Importance

Changes 0
Metric Value
cc 9
eloc 24
nc 6
nop 2
dl 23
loc 41
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Files_Sharing\Collaboration;
25
26
27
use OCP\Collaboration\AutoComplete\ISorter;
28
use OCP\Files\Folder;
29
use OCP\Files\IRootFolder;
30
use OCP\Files\Node;
31
use OCP\IUserSession;
32
use OCP\Share\IManager;
33
34
class ShareRecipientSorter implements ISorter {
35
36
	/** @var IManager */
37
	private $shareManager;
38
	/** @var Folder */
39
	private $rootFolder;
40
	/** @var IUserSession */
41
	private $userSession;
42
43
	public function __construct(IManager $shareManager, IRootFolder $rootFolder, IUserSession $userSession) {
44
		$this->shareManager = $shareManager;
45
		$this->rootFolder = $rootFolder;
46
		$this->userSession = $userSession;
47
	}
48
49
	public function getId() {
50
		return 'share-recipients';
51
	}
52
53
	public function sort(array &$sortArray, array $context) {
54
		// let's be tolerant. Comments  uses "files" by default, other usages are often singular
55
		if($context['itemType'] !== 'files' && $context['itemType'] !== 'file') {
56
			return;
57
		}
58
		$user = $this->userSession->getUser();
59
		if($user === null) {
60
			return;
61
		}
62
		$userFolder = $this->rootFolder->getUserFolder($user->getUID());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface OCP\Files\Folder as the method getUserFolder() does only exist in the following implementations of said interface: OC\Files\Node\LazyRoot, OC\Files\Node\Root.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
63
		/** @var Node[] $nodes */
64
		$nodes = $userFolder->getById((int)$context['itemId']);
65
		if(count($nodes) === 0) {
66
			return;
67
		}
68
		$al = $this->shareManager->getAccessList($nodes[0]);
69
70 View Code Duplication
		foreach ($sortArray as $type => &$byType) {
71
			if(!isset($al[$type]) || !is_array($al[$type])) {
72
				continue;
73
			}
74
75
			// at least on PHP 5.6 usort turned out to be not stable. So we add
76
			// the current index to the value and compare it on a draw
77
			$i = 0;
78
			$workArray = array_map(function($element) use (&$i) {
79
				return [$i++, $element];
80
			}, $byType);
81
82
			usort($workArray, function ($a, $b) use ($al, $type) {
83
				$result = $this->compare($a[1], $b[1], $al[$type]);
84
				if($result === 0) {
85
					$result = $a[0] - $b[0];
86
				}
87
				return $result;
88
			});
89
90
			// and remove the index values again
91
			$byType = array_column($workArray, 1);
92
		}
93
	}
94
95
	/**
96
	 * @param array $a
97
	 * @param array $b
98
	 * @param array $al
99
	 * @return int
100
	 */
101
	protected function compare(array $a, array $b, array $al) {
102
		$a = $a['value']['shareWith'];
103
		$b = $b['value']['shareWith'];
104
105
		$valueA = (int)in_array($a, $al, true);
106
		$valueB = (int)in_array($b, $al, true);
107
108
		return $valueB - $valueA;
109
	}
110
}
111