Conditions | 17 |
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 |
||
44 | public function lock(int $storageId, string $internalPath, int $fileId, array $lockInfo) : bool { |
||
45 | if ($fileId <= 0) { |
||
46 | throw new \InvalidArgumentException('Invalid file id'); |
||
47 | } |
||
48 | if (!isset($lockInfo['token'])) { |
||
49 | throw new \InvalidArgumentException('No token provided in $lockInfo'); |
||
50 | } |
||
51 | |||
52 | // We're making the lock timeout 30 minutes |
||
53 | $timeout = 30*60; |
||
54 | if (isset($lockInfo['timeout'])) { |
||
55 | $timeout = $lockInfo['timeout']; |
||
56 | } |
||
57 | $owner = $lockInfo['owner'] ?? null; |
||
58 | if ($owner === null && $this->userSession->isLoggedIn()) { |
||
59 | $user = $this->userSession->getUser(); |
||
60 | if ($user !== null) { |
||
61 | $owner = $user->getDisplayName(); |
||
62 | if ($user->getEMailAddress() !== null) { |
||
63 | $owner .= " <{$user->getEMailAddress()}>"; |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | |||
68 | $locks = $this->lockMapper->getLocksByPath($storageId, $internalPath, false); |
||
69 | $exists = false; |
||
70 | foreach ($locks as $lock) { |
||
71 | if ($lock->getToken() === $lockInfo['token']) { |
||
72 | $exists = true; |
||
73 | $lock->setCreatedAt($this->timeFactory->getTime()); |
||
74 | $lock->setTimeout($timeout); |
||
75 | if ($lock->getOwner() === '' || $lock->getOwner() === null) { |
||
76 | $lock->setOwner($owner); |
||
77 | } |
||
78 | $this->lockMapper->update($lock); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | if ($exists) { |
||
83 | return true; |
||
84 | } |
||
85 | |||
86 | $depth = 0; |
||
87 | if (isset($lockInfo['depth'])) { |
||
88 | $depth = $lockInfo['depth']; |
||
89 | } |
||
90 | $scope = ILock::LOCK_SCOPE_EXCLUSIVE; |
||
91 | if (isset($lockInfo['scope'])) { |
||
92 | $scope = $lockInfo['scope']; |
||
93 | } |
||
94 | |||
95 | $lock = new Lock(); |
||
96 | $lock->setFileId($fileId); |
||
97 | $lock->setCreatedAt($this->timeFactory->getTime()); |
||
98 | $lock->setTimeout($timeout); |
||
99 | $lock->setOwner($owner); |
||
100 | $lock->setToken($lockInfo['token']); |
||
101 | $lock->setScope($scope); |
||
102 | $lock->setDepth($depth); |
||
103 | |||
104 | if ($this->userSession->isLoggedIn()) { |
||
105 | $user = $this->userSession->getUser(); |
||
106 | if ($user !== null) { |
||
107 | $lock->setOwnerAccountId($user->getAccountId()); |
||
|
|||
108 | } |
||
109 | } |
||
110 | $this->lockMapper->insert($lock); |
||
111 | return true; |
||
112 | } |
||
113 | |||
133 |
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: