diff --git a/src/BigPipeTrait.php b/src/BigPipeTrait.php index 7c2141a..52b0fe0 100644 --- a/src/BigPipeTrait.php +++ b/src/BigPipeTrait.php @@ -1,5 +1,7 @@ executeScript('true'); $this->bigPipeNoJS = FALSE; } - catch (UnsupportedDriverActionException $e) { + catch (UnsupportedDriverActionException) { $this->bigPipeNoJS = TRUE; $this ->getSession() ->setCookie(BigPipeStrategy::NOJS_COOKIE, 'true'); } - catch (\Exception $e) { + catch (\Exception) { // Mute exceptions. } } @@ -88,7 +88,7 @@ public function bigPipeBeforeStep(BeforeStepScope $scope): void { ->setCookie(BigPipeStrategy::NOJS_COOKIE, 'true'); } } - catch (DriverException $e) { + catch (DriverException) { // Mute not visited page exception. return; } diff --git a/src/ContentTrait.php b/src/ContentTrait.php index 79cfede..f974081 100644 --- a/src/ContentTrait.php +++ b/src/ContentTrait.php @@ -1,5 +1,7 @@ getAttribute($attribute); if (!empty($attr)) { $attr_found = TRUE; - if (str_contains($attr, strval($value))) { + if (str_contains((string) $attr, strval($value))) { $attr_value_found = TRUE; break; } diff --git a/src/EmailTrait.php b/src/EmailTrait.php index 1c63996..041387d 100644 --- a/src/EmailTrait.php +++ b/src/EmailTrait.php @@ -1,5 +1,7 @@ emailGetCollectedEmails() as $record) { - $email_to = explode(',', $record['to']); + $email_to = explode(',', (string) $record['to']); if (in_array($address, $email_to)) { throw new \Exception(sprintf('An email sent to "%s" retrieved from test email collector.', $address)); } if (!empty($record['headers']['Cc'])) { - $email_cc = explode(',', $record['headers']['Cc']); + $email_cc = explode(',', (string) $record['headers']['Cc']); if (in_array($address, $email_cc)) { throw new \Exception(sprintf('An email cc\'ed to "%s" retrieved from test email collector.', $address)); } } if (!empty($record['headers']['Bcc'])) { - $email_bcc = explode(',', $record['headers']['Bcc']); + $email_bcc = explode(',', (string) $record['headers']['Bcc']); if (in_array($address, $email_bcc)) { throw new \Exception(sprintf('An email bcc\'ed to "%s" retrieved from test email collector.', $address)); } @@ -188,13 +190,13 @@ public function emailAssertNoEmailsWereSentToAddress(string $address): void { */ public function emailAssertEmailHeadersContains(string $header, PyStringNode $string, bool $exact = FALSE): array { $string_value = (string) $string; - $string_value = $exact ? $string_value : trim(preg_replace('/\s+/', ' ', $string_value)); + $string_value = $exact ? $string_value : trim((string) preg_replace('/\s+/', ' ', $string_value)); foreach ($this->emailGetCollectedEmails() as $record) { $header_value = $record['headers'][$header] ?? ''; $header_value = $exact ? $header_value : trim(preg_replace('/\s+/', ' ', $header_value)); - if (str_contains($header_value, $string_value)) { + if (str_contains((string) $header_value, $string_value)) { return $record; } } @@ -217,16 +219,16 @@ public function emailAssertEmailHeadersContainsExact(string $header, PyStringNod * @Then /^an email to "(?P[^"]*)" user is "(?P[^"]*)" with "(?P[^"]*)" content:$/ */ public function emailAssertEmailToUserIsActionWithContent(string $name, string $action, string $field, PyStringNode $string): void { - $user = $name == 'current' && !empty($this->getUserManager()->getCurrentUser()) ? $this->getUserManager()->getCurrentUser() : user_load_by_name($name); + $user = $name === 'current' && !empty($this->getUserManager()->getCurrentUser()) ? $this->getUserManager()->getCurrentUser() : user_load_by_name($name); if (!$user) { throw new \Exception(sprintf('Unable to find a user "%s"', $name)); } - if ($action == 'sent') { + if ($action === 'sent') { $this->emailAssertEmailContains('to', new PyStringNode([$user->mail], 0), TRUE); $this->emailAssertEmailContains($field, $string); } - elseif ($action == 'not sent') { + elseif ($action === 'not sent') { $this->emailAssertEmailNotContains($field, $string); } else { @@ -246,13 +248,13 @@ public function emailAssertEmailContains(string $field, PyStringNode $string, bo } $string = strval($string); - $string = $exact ? $string : trim(preg_replace('/\s+/', ' ', $string)); + $string = $exact ? $string : trim((string) preg_replace('/\s+/', ' ', $string)); foreach (self::emailGetCollectedEmails() as $record) { $field_string = $record[$field] ?? ''; $field_string = $exact ? $field_string : trim(preg_replace('/\s+/', ' ', $field_string)); - if (str_contains($field_string, $string)) { + if (str_contains((string) $field_string, $string)) { return $record; } } @@ -282,12 +284,12 @@ public function emailAssertEmailNotContains(string $field, PyStringNode $string, } $string = strval($string); - $string = $exact ? $string : trim(preg_replace('/\s+/', ' ', $string)); + $string = $exact ? $string : trim((string) preg_replace('/\s+/', ' ', $string)); foreach (self::emailGetCollectedEmails() as $record) { $field_string = $exact ? $record[$field] : trim(preg_replace('/\s+/', ' ', $record[$field])); - if (str_contains($field_string, $string)) { + if (str_contains((string) $field_string, $string)) { throw new \Exception(sprintf('Found record with%s text "%s" in field "%s" retrieved from test record collector, but should not.', ($exact ? ' exact' : ''), $string, $field)); } } @@ -350,14 +352,14 @@ public function emailAssertEmailContainsAttachmentWithName(string $name, PyStrin * Get default mail system value. */ protected static function emailGetMailSystemDefault(string $type = 'default'): mixed { - return \Drupal::config('system.mail')->get("interface.$type"); + return \Drupal::config('system.mail')->get('interface.' . $type); } /** * Set default mail system value. */ protected static function emailSetMailSystemDefault(string $type, mixed $value): void { - \Drupal::configFactory()->getEditable('system.mail')->set("interface.$type", $value)->save(); + \Drupal::configFactory()->getEditable('system.mail')->set('interface.' . $type, $value)->save(); // Maisystem module completely takes over default interface, so we need to // update it as well if the module is installed. @@ -376,14 +378,14 @@ protected static function emailSetMailSystemDefault(string $type, mixed $value): * Get original mail system value. */ protected static function emailGetMailSystemOriginal(string $type = 'default'): mixed { - return \Drupal::config('system.mail_original')->get("interface.$type"); + return \Drupal::config('system.mail_original')->get('interface.' . $type); } /** * Set original mail system value. */ protected static function emailSetMailSystemOriginal(string $type, mixed $value): void { - \Drupal::configFactory()->getEditable('system.mail_original')->set("interface.$type", $value)->save(); + \Drupal::configFactory()->getEditable('system.mail_original')->set('interface.' . $type, $value)->save(); } /** @@ -399,9 +401,9 @@ protected static function emailDeleteMailSystemOriginal(): void { protected function emailGetCollectedEmails(): array { // Directly read data from the database to avoid cache invalidation that // may corrupt the system under test. - $emails = array_map('unserialize', Database::getConnection()->query("SELECT name, value FROM {key_value} WHERE name = 'system.test_mail_collector'")->fetchAllKeyed()); + $emails = array_map(unserialize(...), Database::getConnection()->query("SELECT name, value FROM {key_value} WHERE name = 'system.test_mail_collector'")->fetchAllKeyed()); - $emails = !empty($emails['system.test_mail_collector']) ? $emails['system.test_mail_collector'] : []; + $emails = empty($emails['system.test_mail_collector']) ? [] : $emails['system.test_mail_collector']; if ($this->emailDebug) { $fields = ['to', 'from', 'subject', 'body']; @@ -432,13 +434,13 @@ protected function emailGetCollectedEmails(): array { protected static function emailExtractLinks(string $string): array { // Correct links before extraction. $pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))'; - $string = preg_replace_callback("#$pattern#i", function (array $matches): string { - return preg_match('!^https?://!i', $matches[0]) ? $matches[0] : "http://$matches[0]"; + $string = preg_replace_callback(sprintf('#%s#i', $pattern), function (array $matches): string { + return preg_match('!^https?://!i', $matches[0]) ? $matches[0] : 'http://' . $matches[0]; }, $string); - preg_match_all("#$pattern#i", $string, $matches); + preg_match_all(sprintf('#%s#i', $pattern), (string) $string, $matches); - return !empty($matches[0]) ? $matches[0] : []; + return empty($matches[0]) ? [] : $matches[0]; } /** @@ -448,14 +450,9 @@ protected static function emailExtractTypes(array $tags): array { $types = []; foreach ($tags as $tag) { - if (str_starts_with($tag, 'email')) { - $parts = explode(':', $tag); - if (count($parts) > 1) { - $types[] = implode(':', array_slice($parts, 1)); - } - else { - $types[] = 'default'; - } + if (str_starts_with((string) $tag, 'email')) { + $parts = explode(':', (string) $tag); + $types[] = count($parts) > 1 ? implode(':', array_slice($parts, 1)) : 'default'; } } diff --git a/src/FieldTrait.php b/src/FieldTrait.php index 19b26b0..d41b116 100644 --- a/src/FieldTrait.php +++ b/src/FieldTrait.php @@ -1,5 +1,7 @@ fieldAssertExists($field_name); } else { @@ -95,10 +97,10 @@ public function fieldAssertExistence(string $field_name, string $exists): void { public function fieldAssertState(string $field_name, string $disabled): void { $field = $this->fieldAssertExists($field_name); - if ($disabled == 'disabled' && !$field->hasAttribute('disabled')) { + if ($disabled === 'disabled' && !$field->hasAttribute('disabled')) { throw new \Exception(sprintf('A field "%s" should be disabled, but it is not.', $field_name)); } - elseif ($disabled != 'disabled' && $field->hasAttribute('disabled')) { + elseif ($disabled !== 'disabled' && $field->hasAttribute('disabled')) { throw new \Exception(sprintf('A field "%s" should not be disabled, but it is.', $field_name)); } } @@ -114,7 +116,7 @@ public function fieldAssertState(string $field_name, string $disabled): void { * @Then field :name should be :presence on the page and have state :state */ public function fieldAssertExistsState(string $field_name, string $presence, string $state = 'enabled'): void { - if ($presence == 'present') { + if ($presence === 'present') { $this->fieldAssertExists($field_name); $this->fieldAssertState($field_name, $state); } diff --git a/src/FileDownloadTrait.php b/src/FileDownloadTrait.php index 7b78da1..e8354a5 100644 --- a/src/FileDownloadTrait.php +++ b/src/FileDownloadTrait.php @@ -1,5 +1,7 @@ getSession()->getPage(); $link_element = $page->findLink($link); @@ -145,7 +147,7 @@ public function fileDownloadAssertFileContains(PyStringNode $string): void { if (!$this->fileDownloadDownloadedFileInfo) { throw new \RuntimeException('Downloaded file content has no data.'); } - $lines = preg_split('/\R/', $this->fileDownloadDownloadedFileInfo['content']); + $lines = preg_split('/\R/', (string) $this->fileDownloadDownloadedFileInfo['content']); foreach ($lines as $line) { if (preg_match('/^\/.+\/[a-z]*$/i', $string)) { if (preg_match($string, $line)) { @@ -297,7 +299,7 @@ protected function fileDownloadProcess(string $url, array $options = []): array $url_file_name = parse_url($url, PHP_URL_PATH); $url_file_name = $url_file_name ? basename($url_file_name) : $url_file_name; $headers['file_name'] = empty($headers['file_name']) && !empty($url_file_name) ? $url_file_name : $headers['file_name']; - $file_path = !empty($headers['file_name']) ? $dir . DIRECTORY_SEPARATOR . $headers['file_name'] : tempnam($dir, 'behat'); + $file_path = empty($headers['file_name']) ? tempnam($dir, 'behat') : $dir . DIRECTORY_SEPARATOR . $headers['file_name']; $file_name = basename($file_path); // Write file contents. @@ -323,12 +325,12 @@ protected function fileDownloadProcess(string $url, array $options = []): array protected function fileDownloadParseHeaders(array $headers): array { $parsed_headers = []; foreach ($headers as $header) { - if (preg_match('/Content-Disposition:\s*attachment;\s*filename\s*=\s*\"([^"]+)"/', $header, $matches) && isset($matches[1])) { + if (preg_match('/Content-Disposition:\s*attachment;\s*filename\s*=\s*\"([^"]+)"/', (string) $header, $matches) && isset($matches[1])) { $parsed_headers['file_name'] = trim($matches[1]); continue; } - if (preg_match('/Content-Type:\s*(.+)/', $header, $matches) && isset($matches[1])) { + if (preg_match('/Content-Type:\s*(.+)/', (string) $header, $matches) && isset($matches[1])) { $parsed_headers['content_type'] = trim($matches[1]); continue; } diff --git a/src/FileTrait.php b/src/FileTrait.php index 0c32863..fbe8c37 100644 --- a/src/FileTrait.php +++ b/src/FileTrait.php @@ -1,10 +1,13 @@ writeData(file_get_contents($path), $destination, FileSystemInterface::EXISTS_REPLACE); + $entity = \Drupal::service('file.repository')->writeData(file_get_contents($path), $destination, FileExists::Replace); $fields = get_object_vars($stub); foreach ($fields as $property => $value) { @@ -123,6 +126,7 @@ protected function fileCreateEntity(\StdClass $stub): FileInterface { } $entity->save(); + return $entity; } diff --git a/src/JsTrait.php b/src/JsTrait.php index df7778f..3ae8c5e 100644 --- a/src/JsTrait.php +++ b/src/JsTrait.php @@ -1,5 +1,7 @@ evaluateScript($script); diff --git a/src/KeyboardTrait.php b/src/KeyboardTrait.php index 46030eb..3715a54 100644 --- a/src/KeyboardTrait.php +++ b/src/KeyboardTrait.php @@ -1,5 +1,7 @@ invokeArgs($withSynResult, [ $xpath, - "syn.key({{ELEMENT}}, '$key');", + sprintf("syn.key({{ELEMENT}}, '%s');", $key), ]); } diff --git a/src/LinkTrait.php b/src/LinkTrait.php index 21f6190..6f6a722 100644 --- a/src/LinkTrait.php +++ b/src/LinkTrait.php @@ -1,5 +1,7 @@ getAttribute('href'))) { + if (!preg_match($pattern, (string) $link->getAttribute('href'))) { throw new \Exception(sprintf('The link href "%s" does not match the specified href "%s"', $link->getAttribute('href'), $href)); } } @@ -98,7 +100,7 @@ public function linkAssertTextHrefNotExists(string $text, string $href, string $ $pattern = '/' . preg_quote($href, '/') . '/'; // Support for simplified wildcard using '*'. $pattern = str_contains($href, '*') ? str_replace('\*', '.*', $pattern) : $pattern; - if (preg_match($pattern, $link->getAttribute('href'))) { + if (preg_match($pattern, (string) $link->getAttribute('href'))) { throw new \Exception(sprintf('The link href "%s" matches the specified href "%s" but should not', $link->getAttribute('href'), $href)); } } @@ -156,7 +158,7 @@ public function assertLinkAbsolute(string $text): void { throw new \Exception(sprintf('The link "%s" is not found', $text)); } $href = $link->getAttribute('href'); - if (!parse_url($href, PHP_URL_SCHEME)) { + if (!parse_url((string) $href, PHP_URL_SCHEME)) { throw new \Exception(sprintf('The link "%s" is not an absolute link.', $text)); } } @@ -172,7 +174,7 @@ public function assertLinkNotAbsolute(string $text): void { throw new \Exception(sprintf('The link "%s" is not found', $text)); } $href = $link->getAttribute('href'); - if (parse_url($href, PHP_URL_SCHEME)) { + if (parse_url((string) $href, PHP_URL_SCHEME)) { throw new \Exception(sprintf('The link "%s" is an absolute link.', $text)); } } diff --git a/src/MediaTrait.php b/src/MediaTrait.php index 60cf6f1..bf6578f 100644 --- a/src/MediaTrait.php +++ b/src/MediaTrait.php @@ -1,5 +1,7 @@ bundle) || !$stub->bundle) { + if (!property_exists($stub, 'bundle') || $stub->bundle === NULL || !$stub->bundle) { throw new \Exception("Cannot create media because it is missing the required property 'bundle'."); } $bundles = \Drupal::getContainer()->get('entity_type.bundle.info')->getBundleInfo('media'); if (!in_array($stub->bundle, array_keys($bundles))) { - throw new \Exception("Cannot create media because provided bundle '$stub->bundle' does not exist."); + throw new \Exception(sprintf("Cannot create media because provided bundle '%s' does not exist.", $stub->bundle)); } $this->mediaExpandEntityFieldsFixtures($stub); @@ -141,7 +143,7 @@ protected function mediaCreateEntity(\StdClass $stub) { protected function mediaExpandEntityFields(string $entity_type, \StdClass $stub) { $core = $this->getDriver()->getCore(); - $class = new \ReflectionClass(get_class($core)); + $class = new \ReflectionClass($core::class); $method = $class->getMethod('expandEntityFields'); $method->setAccessible(TRUE); @@ -165,10 +167,8 @@ protected function mediaExpandEntityFieldsFixtures(\StdClass $stub) { if (!empty($field_types[$name]) && $field_types[$name] == 'image') { if (is_array($value)) { - if (!empty($value[0])) { - if (is_file($fixture_path . $value[0])) { - $stub->{$name}[0] = $fixture_path . $value[0]; - } + if (!empty($value[0]) && is_file($fixture_path . $value[0])) { + $stub->{$name}[0] = $fixture_path . $value[0]; } } elseif (is_file($fixture_path . $value)) { diff --git a/src/MenuTrait.php b/src/MenuTrait.php index 2f61f0e..35d69c0 100644 --- a/src/MenuTrait.php +++ b/src/MenuTrait.php @@ -1,5 +1,7 @@ loadMenuByLabel($label); $menu->delete(); } - catch (\Exception $e) { + catch (\Exception) { } } } @@ -65,7 +67,7 @@ public function menuCreate(TableNode $table): void { foreach ($table->getHash() as $menu_hash) { if (empty($menu_hash['id'])) { // Create menu id if one not provided. - $menu_id = strtolower($menu_hash['label']); + $menu_id = strtolower((string) $menu_hash['label']); $menu_id = preg_replace('/[^a-z0-9_]+/', '_', $menu_id); $menu_id = preg_replace('/_+/', '_', $menu_id); $menu_hash['id'] = $menu_id; @@ -142,7 +144,7 @@ public function menuCleanAll(AfterScenarioScope $scope): void { try { $menu->delete(); } - catch (\Exception $exception) { + catch (\Exception) { // Ignore the exception and move on. continue; } @@ -152,7 +154,7 @@ public function menuCleanAll(AfterScenarioScope $scope): void { try { $menu_link->delete(); } - catch (\Exception $exception) { + catch (\Exception) { // Ignore the exception and move on. continue; } diff --git a/src/OverrideTrait.php b/src/OverrideTrait.php index 8442f47..2220246 100644 --- a/src/OverrideTrait.php +++ b/src/OverrideTrait.php @@ -1,5 +1,7 @@ userManager->getCurrentUser())) { $this->logout(); } diff --git a/src/ParagraphsTrait.php b/src/ParagraphsTrait.php index 65d39e1..8afb30a 100644 --- a/src/ParagraphsTrait.php +++ b/src/ParagraphsTrait.php @@ -1,5 +1,7 @@ delete(); } - catch (\Exception $exception) { + catch (\Exception) { // Ignore the exception and move on. continue; } @@ -155,7 +157,7 @@ protected function paragraphsFindEntity(array $conditions = []): ContentEntityIn protected function paragraphsExpandEntityFields(string $entity_type, \StdClass $stub) { $core = $this->getDriver()->getCore(); - $class = new \ReflectionClass(get_class($core)); + $class = new \ReflectionClass($core::class); $method = $class->getMethod('expandEntityFields'); $method->setAccessible(TRUE); diff --git a/src/PathTrait.php b/src/PathTrait.php index 1f638a6..d6cb1bf 100644 --- a/src/PathTrait.php +++ b/src/PathTrait.php @@ -1,5 +1,7 @@ getSession()->getCurrentUrl(); - $current_path = parse_url($current_path, PHP_URL_PATH); + $current_path = parse_url((string) $current_path, PHP_URL_PATH); $current_path = ltrim($current_path, '/'); - $current_path = $current_path == '' ? '' : $current_path; + $current_path = $current_path === '' ? '' : $current_path; - if ($current_path != ltrim($path, '/')) { + if ($current_path !== ltrim($path, '/')) { throw new \Exception(sprintf('Current path is "%s", but expected is "%s"', $current_path, $path)); } } @@ -48,11 +50,11 @@ public function pathAssertCurrent(string $path): void { */ public function pathAssertNotCurrent(string $path): bool { $current_path = $this->getSession()->getCurrentUrl(); - $current_path = parse_url($current_path, PHP_URL_PATH); + $current_path = parse_url((string) $current_path, PHP_URL_PATH); $current_path = ltrim($current_path, '/'); - $current_path = $current_path == '' ? '' : $current_path; + $current_path = $current_path === '' ? '' : $current_path; - if ($current_path == $path) { + if ($current_path === $path) { throw new \Exception(sprintf('Current path should not be "%s"', $current_path)); } @@ -73,7 +75,7 @@ public function pathAssertVisitWithBasicAuth(string $can, string $path, string $ $this->getSession()->setBasicAuth($user, $pass); $this->visitPath($path); - if ($can == 'can') { + if ($can === 'can') { $this->assertSession()->statusCodeEquals(200); } else { diff --git a/src/ResponseTrait.php b/src/ResponseTrait.php index 41eca01..a7fa8fb 100644 --- a/src/ResponseTrait.php +++ b/src/ResponseTrait.php @@ -1,5 +1,7 @@ id(), $permissions); - $role = Role::load($role->id()); - return $role; + return Role::load($role->id()); } throw new \RuntimeException(sprintf('Failed to create a role with "%s" permission(s).', implode(', ', $permissions))); diff --git a/src/SearchApiTrait.php b/src/SearchApiTrait.php index 60fcc50..2de1e20 100644 --- a/src/SearchApiTrait.php +++ b/src/SearchApiTrait.php @@ -1,5 +1,7 @@ $index) { + foreach ($indexes as $index) { $index->indexItems($limit); } } diff --git a/src/SelectTrait.php b/src/SelectTrait.php index 517d8cd..e64f7fa 100644 --- a/src/SelectTrait.php +++ b/src/SelectTrait.php @@ -1,5 +1,7 @@ visitPath("/user/$id/edit"); + $this->visitPath(sprintf('/user/%s/edit', $id)); } /** @@ -69,7 +71,7 @@ public function userDelete(TableNode $usersTable): void { $user = $this->userGetByName($userHash['name']); } } - catch (\Exception $exception) { + catch (\Exception) { // User may not exist - do nothing. } @@ -93,7 +95,7 @@ public function userAssertHasRoles(string $name, string $roles): void { return trim($value); }, $roles); - if (count(array_intersect($roles, $user->getRoles())) != count($roles)) { + if (count(array_intersect($roles, $user->getRoles())) !== count($roles)) { throw new \Exception(sprintf('User "%s" does not have role(s) "%s", but has roles "%s".', $name, implode('", "', $roles), implode('", "', $user->getRoles()))); } } @@ -122,7 +124,7 @@ public function userAssertHasNoRoles(string $name, string $roles): void { * @Then user :name has :status status */ public function userAssertHasStatus(string $name, string $status): void { - $status = $status == 'active'; + $status = $status === 'active'; $user = $this->userGetByName($name); @@ -145,11 +147,11 @@ public function userSetPassword(string $name, string $password): void { /** @var \Drupal\user\UserInterface $user */ $user = $this->userGetByName($name); } - catch (\Exception $e1) { + catch (\Exception) { try { $user = $this->userGetByMail($name); } - catch (\Exception $e2) { + catch (\Exception) { throw new \Exception(sprintf('Unable to find a user with name or email "%s".', $name)); } } diff --git a/src/VisibilityTrait.php b/src/VisibilityTrait.php index efa892f..e95d402 100644 --- a/src/VisibilityTrait.php +++ b/src/VisibilityTrait.php @@ -1,5 +1,7 @@ evaluateScript('true'); } - catch (UnsupportedDriverActionException $exception) { + catch (UnsupportedDriverActionException) { throw new UnsupportedDriverActionException('Method can be used only with JS-capable driver', $driver); } diff --git a/src/WatchdogTrait.php b/src/WatchdogTrait.php index 9af6757..e183023 100644 --- a/src/WatchdogTrait.php +++ b/src/WatchdogTrait.php @@ -1,5 +1,7 @@ strlen($prefix)) { - $types[] = substr($tag, strlen($prefix)); + if (str_starts_with((string) $tag, $prefix) && strlen((string) $tag) > strlen($prefix)) { + $types[] = substr((string) $tag, strlen($prefix)); } } diff --git a/src/WysiwygTrait.php b/src/WysiwygTrait.php index 569380e..67a3d1b 100644 --- a/src/WysiwygTrait.php +++ b/src/WysiwygTrait.php @@ -1,5 +1,7 @@ evaluateScript('true'); } - catch (UnsupportedDriverActionException $exception) { + catch (UnsupportedDriverActionException) { // For non-JS drivers process field in a standard way. $element->setValue($value); return; @@ -54,21 +56,21 @@ public function wysiwygFillField(string $field, string $value): void { $is_ckeditor_4 = !empty($driver->find($parent_element->getXpath() . "/div[contains(@class,'cke')]")); if ($is_ckeditor_4) { $this->getSession() - ->executeScript("CKEDITOR.instances[\"$element_id\"].setData(\"$value\");"); + ->executeScript(sprintf('CKEDITOR.instances["%s"].setData("%s");', $element_id, $value)); return; } // Support Ckeditor 5. - $ckeditor_5_element_selector = ".{$parent_element->getAttribute('class')} .ck-editor__editable"; + $ckeditor_5_element_selector = sprintf('.%s .ck-editor__editable', $parent_element->getAttribute('class')); $this->getSession() ->executeScript( " - const domEditableElement = document.querySelector(\"$ckeditor_5_element_selector\"); + const domEditableElement = document.querySelector(\"{$ckeditor_5_element_selector}\"); if (domEditableElement.ckeditorInstance) { const editorInstance = domEditableElement.ckeditorInstance; if (editorInstance) { - editorInstance.setData(\"$value\"); + editorInstance.setData(\"{$value}\"); } else { throw new Exception('Could not get the editor instance!'); } diff --git a/tests/behat/fixtures/d10/rector.php b/tests/behat/fixtures/d10/rector.php index a59c9ce..0e8db3c 100644 --- a/tests/behat/fixtures/d10/rector.php +++ b/tests/behat/fixtures/d10/rector.php @@ -19,9 +19,7 @@ use Rector\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector; use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector; use Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector; -use Rector\CodingStyle\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector; use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector; -use Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector; use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector; use Rector\Config\RectorConfig; use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector; @@ -61,13 +59,11 @@ $rectorConfig->skip([ // Rules added by Rector's rule sets. - ArraySpreadInsteadOfArrayMergeRector::class, CountArrayToEmptyArrayComparisonRector::class, DisallowedEmptyRuleFixerRector::class, InlineArrayReturnAssignRector::class, NewlineAfterStatementRector::class, NewlineBeforeNewAssignSetRector::class, - PostIncDecToPreIncDecRector::class, RemoveAlwaysTrueIfConditionRector::class, SimplifyEmptyCheckOnEmptyArrayRector::class, // Dependencies.