Completed
Push — master ( ed3b9d...3835d9 )
by Roeland
20:08
created

SharedScanner::getSourceScanner()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 3
eloc 9
nc 3
nop 0
1
<?php
2
/**
3
 * @author Morris Jobke <[email protected]>
4
 * @author Robin Appelman <[email protected]>
5
 * @author Vincent Petry <[email protected]>
6
 *
7
 * @copyright Copyright (c) 2016, ownCloud, Inc.
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
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, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\Files_Sharing;
25
26
use OC\Files\ObjectStore\NoopScanner;
27
28
/**
29
 * Scanner for SharedStorage
30
 */
31
class Scanner extends \OC\Files\Cache\Scanner {
32
	private $sourceScanner;
33
34
	/**
35
	 * Returns metadata from the shared storage, but
36
	 * with permissions from the source storage.
37
	 *
38
	 * @param string $path path of the file for which to retrieve metadata
39
	 *
40
	 * @return array an array of metadata of the file
41
	 */
42
	public function getData($path) {
43
		$data = parent::getData($path);
44
		if ($data === null) {
45
			return null;
46
		}
47
		list($sourceStorage, $internalPath) = $this->storage->resolvePath($path);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface OC\Files\Storage\Storage as the method resolvePath() does only exist in the following implementations of said interface: OC\Files\Storage\Shared, OC\Files\Storage\Wrapper\Jail.

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...
48
		$data['permissions'] = $sourceStorage->getPermissions($internalPath);
49
		return $data;
50
	}
51
52
	private function getSourceScanner() {
53
		if ($this->sourceScanner) {
54
			return $this->sourceScanner;
55
		}
56
		if ($this->storage->instanceOfStorage('\OC\Files\Storage\Shared')) {
57
			/** @var \OC\Files\Storage\Storage $storage */
58
			list($storage) = $this->storage->resolvePath('');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface OC\Files\Storage\Storage as the method resolvePath() does only exist in the following implementations of said interface: OC\Files\Storage\Shared, OC\Files\Storage\Wrapper\Jail.

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...
59
			$this->sourceScanner = $storage->getScanner();
60
			return $this->sourceScanner;
61
		} else {
62
			return null;
63
		}
64
	}
65
66
	public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true) {
67
		$sourceScanner = $this->getSourceScanner();
68
		if ($sourceScanner instanceof NoopScanner) {
69
			return [];
70
		} else {
71
			return parent::scanFile($file, $reuseExisting, $parentId, $cacheData, $lock);
72
		}
73
	}
74
}
75
76