From b8566a3a0d12c496486cd8a06eabe597a1dcc4c3 Mon Sep 17 00:00:00 2001 From: Faraz Samapoor Date: Sun, 7 May 2023 09:30:01 +0330 Subject: [PATCH] Converts 'strpos()' calls to improve code readability. Signed-off-by: Faraz Samapoor --- lib/Controller/AttachmentController.php | 8 ++++---- lib/Db/RelationalEntity.php | 6 +++--- lib/Listeners/BeforeTemplateRenderedListener.php | 4 ++-- lib/Listeners/ResourceAdditionalScriptsListener.php | 2 +- lib/Middleware/ExceptionMiddleware.php | 2 +- lib/Search/FilterStringParser.php | 4 ++-- lib/Validators/BaseValidator.php | 2 +- tests/unit/Activity/ActivityManagerTest.php | 2 +- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/Controller/AttachmentController.php b/lib/Controller/AttachmentController.php index 38334199a..82e000e81 100644 --- a/lib/Controller/AttachmentController.php +++ b/lib/Controller/AttachmentController.php @@ -53,7 +53,7 @@ public function getAll($cardId) { * @throws \OCA\Deck\NotFoundException */ public function display($cardId, $attachmentId) { - if (strpos($attachmentId, ':') === false) { + if (!str_contains($attachmentId, ':')) { $type = 'deck_file'; } else { [$type, $attachmentId] = explode(':', $attachmentId); @@ -76,7 +76,7 @@ public function create($cardId) { * @NoAdminRequired */ public function update($cardId, $attachmentId) { - if (strpos($attachmentId, ':') === false) { + if (!str_contains($attachmentId, ':')) { $type = 'deck_file'; } else { [$type, $attachmentId] = explode(':', $attachmentId); @@ -88,7 +88,7 @@ public function update($cardId, $attachmentId) { * @NoAdminRequired */ public function delete($cardId, $attachmentId) { - if (strpos($attachmentId, ':') === false) { + if (!str_contains($attachmentId, ':')) { $type = 'deck_file'; } else { [$type, $attachmentId] = explode(':', $attachmentId); @@ -100,7 +100,7 @@ public function delete($cardId, $attachmentId) { * @NoAdminRequired */ public function restore($cardId, $attachmentId) { - if (strpos($attachmentId, ':') === false) { + if (!str_contains($attachmentId, ':')) { $type = 'deck_file'; } else { [$type, $attachmentId] = explode(':', $attachmentId); diff --git a/lib/Db/RelationalEntity.php b/lib/Db/RelationalEntity.php index 683f1611f..8c27daba5 100644 --- a/lib/Db/RelationalEntity.php +++ b/lib/Db/RelationalEntity.php @@ -68,7 +68,7 @@ public function jsonSerialize(): array { $reflection = new \ReflectionClass($this); $json = []; foreach ($properties as $property => $value) { - if (strpos($property, '_') !== 0 && $reflection->hasProperty($property)) { + if (!str_starts_with($property, '_') && $reflection->hasProperty($property)) { $propertyReflection = $reflection->getProperty($property); if (!$propertyReflection->isPrivate() && !in_array($property, $this->_resolvedProperties, true)) { $json[$property] = $this->getter($property); @@ -129,7 +129,7 @@ public function resolveRelation($property, $resolver) { public function __call(string $methodName, array $args) { $attr = lcfirst(substr($methodName, 7)); - if (array_key_exists($attr, $this->_resolvedProperties) && strpos($methodName, 'resolve') === 0) { + if (array_key_exists($attr, $this->_resolvedProperties) && str_starts_with($methodName, 'resolve')) { if ($this->_resolvedProperties[$attr] !== null) { return $this->_resolvedProperties[$attr]; } @@ -137,7 +137,7 @@ public function __call(string $methodName, array $args) { } $attr = lcfirst(substr($methodName, 3)); - if (array_key_exists($attr, $this->_resolvedProperties) && strpos($methodName, 'set') === 0) { + if (array_key_exists($attr, $this->_resolvedProperties) && str_starts_with($methodName, 'set')) { if (!is_scalar($args[0])) { $args[0] = $args[0]['primaryKey']; } diff --git a/lib/Listeners/BeforeTemplateRenderedListener.php b/lib/Listeners/BeforeTemplateRenderedListener.php index 9aaae45c5..033c5efde 100644 --- a/lib/Listeners/BeforeTemplateRenderedListener.php +++ b/lib/Listeners/BeforeTemplateRenderedListener.php @@ -51,11 +51,11 @@ public function handle(Event $event): void { Util::addStyle('deck', 'deck'); $pathInfo = $this->request->getPathInfo(); - if (strpos($pathInfo, '/apps/calendar') === 0) { + if (str_starts_with($pathInfo, '/apps/calendar')) { Util::addScript('deck', 'deck-calendar'); } - if (strpos($pathInfo, '/call/') === 0 || strpos($pathInfo, '/apps/spreed') === 0) { + if (str_starts_with($pathInfo, '/call/') || str_starts_with($pathInfo, '/apps/spreed')) { Util::addScript('deck', 'deck-talk'); } } diff --git a/lib/Listeners/ResourceAdditionalScriptsListener.php b/lib/Listeners/ResourceAdditionalScriptsListener.php index 328d697aa..0ca63f2b4 100644 --- a/lib/Listeners/ResourceAdditionalScriptsListener.php +++ b/lib/Listeners/ResourceAdditionalScriptsListener.php @@ -21,7 +21,7 @@ public function handle(Event $event): void { return; } - if (strpos($this->request->getPathInfo(), '/call/') === 0) { + if (str_starts_with($this->request->getPathInfo(), '/call/')) { // Talk integration has its own entrypoint which already includes collections handling return; } diff --git a/lib/Middleware/ExceptionMiddleware.php b/lib/Middleware/ExceptionMiddleware.php index 0b0dc013e..48a5f3e24 100644 --- a/lib/Middleware/ExceptionMiddleware.php +++ b/lib/Middleware/ExceptionMiddleware.php @@ -109,7 +109,7 @@ public function afterException($controller, $methodName, \Exception $exception) ], $exception->getStatus()); } - if (strpos(get_class($controller), 'OCA\\Deck\\Controller\\') === 0) { + if (str_starts_with(get_class($controller), 'OCA\\Deck\\Controller\\')) { $response = [ 'status' => 500, 'message' => $exceptionMessage, diff --git a/lib/Search/FilterStringParser.php b/lib/Search/FilterStringParser.php index 08f769082..9c576ff4b 100644 --- a/lib/Search/FilterStringParser.php +++ b/lib/Search/FilterStringParser.php @@ -41,7 +41,7 @@ class FilterStringParser { public function __construct(IL10N $l10n) { $this->l10n = $l10n; } - + public function parse(?string $filter): SearchQuery { $query = new SearchQuery(); if (empty($filter)) { @@ -71,7 +71,7 @@ public function parse(?string $filter): SearchQuery { } private function parseFilterToken(SearchQuery $query, string $token): bool { - if (strpos($token, ':') === false) { + if (!str_contains($token, ':')) { return false; } diff --git a/lib/Validators/BaseValidator.php b/lib/Validators/BaseValidator.php index 5601a84a5..220695da6 100644 --- a/lib/Validators/BaseValidator.php +++ b/lib/Validators/BaseValidator.php @@ -55,7 +55,7 @@ private function validate($data) { // The format for specifying validation rules and parameters follows an // easy {rule}:{parameters} formatting convention. For instance the // rule "Max:3" states that the value may only be three letters. - if (strpos($rule, ':') !== false) { + if (str_contains($rule, ':')) { [$rule, $parameter] = explode(':', $rule, 2); if (!$this->{$rule}($value, $parameter)) { throw new BadRequestException( diff --git a/tests/unit/Activity/ActivityManagerTest.php b/tests/unit/Activity/ActivityManagerTest.php index 700d72b19..332558f51 100644 --- a/tests/unit/Activity/ActivityManagerTest.php +++ b/tests/unit/Activity/ActivityManagerTest.php @@ -98,7 +98,7 @@ public function testGetActivityFormatOwn() { ->willReturn($this->l10n); foreach ($managerClass->getConstants() as $constant => $value) { - if (strpos($constant, 'SUBJECT') === 0) { + if (str_starts_with($constant, 'SUBJECT')) { $format = $this->activityManager->getActivityFormat('cz', $value, [], false); if ($format !== '') { $this->assertStringContainsString('{user}', $format);