Completed
Pull Request — master (#31651)
by Thomas
34:46 queued 22:41
created

Lock   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 115
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setToken() 0 4 1
A getOwner() 0 3 1
A getFileId() 0 3 1
A getTimeout() 0 3 1
A getCreatedAt() 0 3 1
A getToken() 0 3 1
A getScope() 0 3 1
A getDepth() 0 3 1
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 * @copyright Copyright (c) 2018, ownCloud GmbH
5
 * @license AGPL-3.0
6
 *
7
 * This code is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License, version 3,
9
 * as published by the Free Software Foundation.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License, version 3,
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
18
 */
19
20
namespace OC\Lock\Persistent;
21
22
use OCP\AppFramework\Db\Entity;
23
use OCP\Lock\Persistent\ILock;
24
25
/**
26
 * Class Lock
27
 *
28
 * @method string getTokenHash()
29
 * @method string getPath()
30
 * @method string getUriV1()
31
 * @method string getUriV2()
32
 * @method string getOwnerAccountId()
33
 *
34
 * @method setFileId(int $fileId)
35
 * @method setCreatedAt(int $timestamp)
36
 * @method setTimeout(int $timeout)
37
 * @method setScope(int $scope)
38
 * @method setDepth(int $depth)
39
 *
40
 * @package OC\Lock\Persistent
41
 */
42
class Lock extends Entity implements ILock {
43
44
	/** @var int BIGINT - foreign key to oc_filecache.fileid */
45
	protected $fileId;
46
	/** @var string - plain text field as transmitted by clients */
47
	protected $owner;
48
	/** @var int - seconds of lock life time */
49
	protected $timeout;
50
	/** @var int - unix timestamp when lock was created */
51
	protected $createdAt;
52
	/** @var string - uuid in WebDAV */
53
	protected $token;
54
	/** @var string - md5 of token */
55
	protected $tokenHash;
56
	/** @var int - LOCK_SCOPE_EXCLUSIVE or LOCK_SCOPE_SHARED */
57
	protected $scope;
58
	/** @var int: 0, 1 or infinite */
59
	protected $depth;
60
	/** @var int - foreign key zu oc_account.id */
61
	protected $ownerAccountId;
62
63
	/** @var string - joined with oc_filecache */
64
	protected $path;
65
	/** @var string - computed value */
66
	protected $globalUserId;
67
	/** @var string - computed value */
68
	protected $globalFileName;
69
70
	public function __construct() {
71
		$this->addType('fileId', 'integer');
72
		$this->addType('timeout', 'integer');
73
		$this->addType('createdAt', 'integer');
74
		$this->addType('scope', 'integer');
75
		$this->addType('depth', 'integer');
76
		$this->addType('ownerAccountId', 'integer');
77
	}
78
79
	/**
80
	 * @param $token
81
	 */
82
	public function setToken($token) {
83
		parent::setter('token', [$token]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setter() instead of setToken()). Are you sure this is correct? If so, you might want to change this to $this->setter().

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...
84
		parent::setter('tokenHash', [\md5($token)]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setter() instead of setToken()). Are you sure this is correct? If so, you might want to change this to $this->setter().

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...
85
	}
86
87
	/**
88
	 * Return the owner of the lock - plain text field as transmitted by clients
89
	 *
90
	 * @return string
91
	 * @since 11.0.0
92
	 */
93
	public function getOwner(): ?string {
94
		return parent::getOwner();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OCP\AppFramework\Db\Entity as the method getOwner() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Lock\Persistent\Lock, OC\Tagging\Tag. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
95
	}
96
97
	/**
98
	 * Foreign key to oc_filecache.fileid
99
	 *
100
	 * @return int
101
	 * @since 11.0.0
102
	 */
103
	public function getFileId(): int {
104
		return parent::getFileId();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OCP\AppFramework\Db\Entity as the method getFileId() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Lock\Persistent\Lock. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
105
	}
106
107
	/**
108
	 * Seconds of lock life time
109
	 *
110
	 * @return int
111
	 * @since 11.0.0
112
	 */
113
	public function getTimeout(): int {
114
		return parent::getTimeout();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OCP\AppFramework\Db\Entity as the method getTimeout() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Lock\Persistent\Lock. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
115
	}
116
117
	/**
118
	 * Unix timestamp when lock was created
119
	 *
120
	 * @return mixed
121
	 * @since 11.0.0
122
	 */
123
	public function getCreatedAt(): int {
124
		return parent::getCreatedAt();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OCP\AppFramework\Db\Entity as the method getCreatedAt() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Lock\Persistent\Lock. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
125
	}
126
127
	/**
128
	 * Token to identify the lock - uuid usually
129
	 *
130
	 * @return string
131
	 * @since 11.0.0
132
	 */
133
	public function getToken(): string {
134
		return parent::getToken();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OCP\AppFramework\Db\Entity as the method getToken() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken, OC\Lock\Persistent\Lock. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
135
	}
136
137
	/**
138
	 * Either shared lock or exclusive lock
139
	 *
140
	 * @return int
141
	 * @since 11.0.0
142
	 */
143
	public function getScope(): int {
144
		return parent::getScope();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OCP\AppFramework\Db\Entity as the method getScope() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Lock\Persistent\Lock. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
145
	}
146
147
	/**
148
	 * Depth as used in WebDAV: 0, 1 or infinite
149
	 *
150
	 * @return int
151
	 * @since 11.0.0
152
	 */
153
	public function getDepth(): int {
154
		return parent::getDepth();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OCP\AppFramework\Db\Entity as the method getDepth() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Lock\Persistent\Lock. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
155
	}
156
}
157