| Total Complexity | 41 |
| Total Lines | 214 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CronLock often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CronLock, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class CronLock |
||
| 18 | { |
||
| 19 | private string $lockDirectory; |
||
| 20 | private string $taskName; |
||
| 21 | private string $lockFile; |
||
| 22 | |||
| 23 | /** @var resource|null */ |
||
| 24 | private $lockHandle = null; |
||
| 25 | |||
| 26 | private bool $ownsLock = false; |
||
| 27 | private int $maxLockAge; |
||
| 28 | |||
| 29 | private const DEFAULT_MAX_LOCK_AGE = 86400; |
||
| 30 | |||
| 31 | public function __construct(string $taskName, ?string $lockDirectory = null, ?int $maxLockAge = null) |
||
| 32 | { |
||
| 33 | $this->taskName = $this->sanitizeTaskName($taskName); |
||
| 34 | $this->lockDirectory = $this->resolveLockDirectory($lockDirectory); |
||
| 35 | $this->lockFile = $this->lockDirectory . DS . $this->taskName . '.lock'; |
||
| 36 | $this->maxLockAge = $maxLockAge ?? (int) cron_config('max_lock_age', self::DEFAULT_MAX_LOCK_AGE); |
||
| 37 | |||
| 38 | $this->ensureLockDirectoryExists(); |
||
| 39 | $this->cleanupStaleLocks(); |
||
| 40 | } |
||
| 41 | |||
| 42 | public function acquire(): bool |
||
| 43 | { |
||
| 44 | $this->lockHandle = @fopen($this->lockFile, 'c+'); |
||
|
|
|||
| 45 | if ($this->lockHandle === false) { |
||
| 46 | $this->lockHandle = null; |
||
| 47 | $this->ownsLock = false; |
||
| 48 | return false; |
||
| 49 | } |
||
| 50 | |||
| 51 | if (!flock($this->lockHandle, LOCK_EX | LOCK_NB)) { |
||
| 52 | fclose($this->lockHandle); |
||
| 53 | $this->lockHandle = null; |
||
| 54 | $this->ownsLock = false; |
||
| 55 | return false; |
||
| 56 | } |
||
| 57 | |||
| 58 | $this->writeTimestampToHandle($this->lockHandle); |
||
| 59 | $this->ownsLock = true; |
||
| 60 | |||
| 61 | return true; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Update lock timestamp (useful for long-running jobs) |
||
| 66 | */ |
||
| 67 | public function refresh(): bool |
||
| 68 | { |
||
| 69 | if (!$this->ownsLock || $this->lockHandle === null) { |
||
| 70 | return false; |
||
| 71 | } |
||
| 72 | |||
| 73 | $this->writeTimestampToHandle($this->lockHandle); |
||
| 74 | return true; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function release(): bool |
||
| 78 | { |
||
| 79 | if (!$this->ownsLock || $this->lockHandle === null) { |
||
| 80 | return true; |
||
| 81 | } |
||
| 82 | |||
| 83 | flock($this->lockHandle, LOCK_UN); |
||
| 84 | fclose($this->lockHandle); |
||
| 85 | |||
| 86 | $this->lockHandle = null; |
||
| 87 | $this->ownsLock = false; |
||
| 88 | |||
| 89 | @fs()->remove($this->lockFile); |
||
| 90 | |||
| 91 | return true; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Check if another process currently holds the lock. |
||
| 96 | */ |
||
| 97 | public function isLocked(): bool |
||
| 98 | { |
||
| 99 | if (!fs()->exists($this->lockFile)) { |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | |||
| 103 | $handle = @fopen($this->lockFile, 'c+'); |
||
| 104 | if ($handle === false) { |
||
| 105 | return true; |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!flock($handle, LOCK_EX | LOCK_NB)) { |
||
| 109 | fclose($handle); |
||
| 110 | return true; |
||
| 111 | } |
||
| 112 | |||
| 113 | flock($handle, LOCK_UN); |
||
| 114 | fclose($handle); |
||
| 115 | |||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | private function sanitizeTaskName(string $taskName): string |
||
| 120 | { |
||
| 121 | $taskName = trim($taskName); |
||
| 122 | if ($taskName === '') { |
||
| 123 | return 'default'; |
||
| 124 | } |
||
| 125 | |||
| 126 | // Keep safe filename chars only |
||
| 127 | $taskName = preg_replace('/[^a-zA-Z0-9._-]+/', '_', $taskName) ?? 'default'; |
||
| 128 | $taskName = trim($taskName, '._-'); |
||
| 129 | |||
| 130 | return $taskName !== '' ? $taskName : 'default'; |
||
| 131 | } |
||
| 132 | |||
| 133 | private function resolveLockDirectory(?string $lockDirectory): string |
||
| 134 | { |
||
| 135 | $path = $lockDirectory ?? cron_config('lock_path'); |
||
| 136 | return $path === null ? $this->getDefaultLockDirectory() : $path; |
||
| 137 | } |
||
| 138 | |||
| 139 | private function getDefaultLockDirectory(): string |
||
| 140 | { |
||
| 141 | return base_dir() . DS . 'runtime' . DS . 'cron' . DS . 'locks'; |
||
| 142 | } |
||
| 143 | |||
| 144 | private function ensureLockDirectoryExists(): void |
||
| 145 | { |
||
| 146 | if ($this->lockDirectory === '') { |
||
| 147 | throw CronException::lockDirectoryNotWritable(''); |
||
| 148 | } |
||
| 149 | |||
| 150 | $this->createDirectory($this->lockDirectory); |
||
| 151 | |||
| 152 | if (!fs()->isWritable($this->lockDirectory)) { |
||
| 153 | throw CronException::lockDirectoryNotWritable($this->lockDirectory); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | private function createDirectory(string $directory): void |
||
| 158 | { |
||
| 159 | if (fs()->isDirectory($directory)) { |
||
| 160 | return; |
||
| 161 | } |
||
| 162 | |||
| 163 | $parent = dirname($directory); |
||
| 164 | if ($parent && $parent !== $directory) { |
||
| 165 | $this->createDirectory($parent); |
||
| 166 | } |
||
| 167 | |||
| 168 | // @phpstan-ignore-next-line |
||
| 169 | if (!fs()->makeDirectory($directory) && !fs()->isDirectory($directory)) { |
||
| 170 | throw CronException::lockDirectoryNotWritable($directory); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Removes stale lock files that are NOT currently locked by any process. |
||
| 176 | * Safe because we take LOCK_EX before removing. |
||
| 177 | */ |
||
| 178 | private function cleanupStaleLocks(): void |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | private function writeTimestampToHandle($handle): void |
||
| 219 | } |
||
| 220 | |||
| 221 | private function readTimestampFromHandle($handle): ?int |
||
| 231 | } |
||
| 232 | } |
||
| 233 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.