Conditions | 16 |
Paths | 418 |
Total Lines | 69 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
40 | public function lock(int $storageId, string $internalPath, int $fileId, array $lockInfo) : bool { |
||
41 | if ($fileId <= 0) { |
||
42 | throw new \InvalidArgumentException('Invalid file id'); |
||
43 | } |
||
44 | if (!isset($lockInfo['token'])) { |
||
45 | throw new \InvalidArgumentException('No token provided in $lockInfo'); |
||
46 | } |
||
47 | |||
48 | // We're making the lock timeout 30 minutes |
||
49 | $timeout = 30*60; |
||
50 | if (isset($lockInfo['timeout'])) { |
||
51 | $timeout = $lockInfo['timeout']; |
||
52 | } |
||
53 | $owner = $lockInfo['owner'] ?? null; |
||
54 | if ($owner === null && $this->userSession->isLoggedIn()) { |
||
55 | $user = $this->userSession->getUser(); |
||
56 | if ($user !== null) { |
||
57 | $owner = $user->getDisplayName(); |
||
58 | if ($user->getEMailAddress() !== null) { |
||
59 | $owner .= " <{$user->getEMailAddress()}>"; |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | |||
64 | $locks = $this->lockMapper->getLocksByPath($storageId, $internalPath, false); |
||
65 | $exists = false; |
||
66 | foreach ($locks as $lock) { |
||
67 | if ($lock->getToken() === $lockInfo['token']) { |
||
68 | $exists = true; |
||
69 | $lock->setCreatedAt(\time()); |
||
70 | $lock->setTimeout($timeout); |
||
71 | if (empty($lock->getOwner())) { |
||
72 | $lock->setOwner($owner); |
||
|
|||
73 | } |
||
74 | $this->lockMapper->update($lock); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | if ($exists) { |
||
79 | return true; |
||
80 | } |
||
81 | |||
82 | $depth = 0; |
||
83 | if (isset($lockInfo['depth'])) { |
||
84 | $depth = $lockInfo['depth']; |
||
85 | } |
||
86 | $scope = ILock::LOCK_SCOPE_EXCLUSIVE; |
||
87 | if (isset($lockInfo['scope'])) { |
||
88 | $scope = $lockInfo['scope']; |
||
89 | } |
||
90 | |||
91 | $lock = new Lock(); |
||
92 | $lock->setFileId($fileId); |
||
93 | $lock->setCreatedAt(\time()); |
||
94 | $lock->setTimeout($timeout); |
||
95 | $lock->setOwner($owner); |
||
96 | $lock->setToken($lockInfo['token']); |
||
97 | $lock->setScope($scope); |
||
98 | $lock->setDepth($depth); |
||
99 | |||
100 | if ($this->userSession->isLoggedIn()) { |
||
101 | $user = $this->userSession->getUser(); |
||
102 | if ($user !== null) { |
||
103 | $lock->setOwnerAccountId($user->getAccountId()); |
||
104 | } |
||
105 | } |
||
106 | $this->lockMapper->insert($lock); |
||
107 | return true; |
||
108 | } |
||
109 | |||
129 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: