From 2e194ce992c3cbd11da8cd8b66ea71fdf8d5557c Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Sun, 15 Oct 2023 16:48:21 +1100 Subject: [PATCH] Fixed several static errors. --- phpstan.neon | 2 +- src/BigPipeTrait.php | 2 +- src/DateTrait.php | 2 +- src/DraggableViewsTrait.php | 10 +++++----- src/ElementTrait.php | 7 +++---- src/FileDownloadTrait.php | 7 ++++--- src/FileTrait.php | 12 ++++-------- src/LinkTrait.php | 2 -- src/ParagraphsTrait.php | 11 +++++------ src/ResponseTrait.php | 4 ++-- src/WatchdogTrait.php | 2 +- src/WysiwygTrait.php | 17 ++++++++--------- tests/behat/bootstrap/FeatureContextTrait.php | 6 +++--- 13 files changed, 38 insertions(+), 46 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index b83e1e4c..8be331ca 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,7 +4,7 @@ parameters: - level: 1 + level: 5 paths: - ../src diff --git a/src/BigPipeTrait.php b/src/BigPipeTrait.php index d6685481..194d098a 100644 --- a/src/BigPipeTrait.php +++ b/src/BigPipeTrait.php @@ -40,7 +40,7 @@ public function bigPipeBeforeScenarioInit(BeforeScenarioScope $scope) { catch (UnsupportedDriverActionException $e) { $this ->getSession() - ->setCookie(BigPipeStrategy::NOJS_COOKIE, TRUE); + ->setCookie(BigPipeStrategy::NOJS_COOKIE, 'true'); } catch (\Exception $e) { // Mute exceptions. diff --git a/src/DateTrait.php b/src/DateTrait.php index 0d8093df..0ce37ff4 100644 --- a/src/DateTrait.php +++ b/src/DateTrait.php @@ -98,7 +98,7 @@ public static function dateRelativeProcessValue($value, $now = NULL) { $timestamp = date($matches[3], $timestamp); } - if ($timestamp === FALSE) { + if (empty($timestamp)) { throw new \RuntimeException(sprintf('The supplied relative date cannot be evaluated: "%s"', $matches[1])); } diff --git a/src/DraggableViewsTrait.php b/src/DraggableViewsTrait.php index ecac121b..f61491f6 100644 --- a/src/DraggableViewsTrait.php +++ b/src/DraggableViewsTrait.php @@ -6,6 +6,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Database\Database; use Drupal\node\Entity\Node; +use Drupal\node\NodeInterface; /** * Trait DraggableViewsTrait. @@ -21,15 +22,14 @@ trait DraggableViewsTrait { * * @Then I save draggable views :view_id view :views_display_id display :bundle items in the following order: */ - public function draggableViewsSaveBundleOrder($view_id, $view_display_id, $bundle, TableNode $order_table) { + public function draggableViewsSaveBundleOrder($view_id, $view_display_id, $bundle, TableNode $order_table): void { $connection = Database::getConnection(); foreach ($order_table->getColumn(0) as $weight => $title) { - /** @var \Drupal\node\Entity\Node $node */ $node = $this->draggableViewsFindNode($bundle, ['title' => $title]); - if (!$node) { - throw new \RuntimeException(sprintf('Unable to find node "%s"', $node->getTitle())); + if (empty($node)) { + throw new \RuntimeException(sprintf('Unable to find node "%s"', $title)); } $entity_id = $node->id(); @@ -63,7 +63,7 @@ public function draggableViewsSaveBundleOrder($view_id, $view_display_id, $bundl /** * Find a node using provided conditions. */ - protected function draggableViewsFindNode($type, $conditions) { + protected function draggableViewsFindNode($type, $conditions): NodeInterface|null { $query = \Drupal::entityQuery('node') ->accessCheck(FALSE) ->condition('type', $type); diff --git a/src/ElementTrait.php b/src/ElementTrait.php index f493d1f1..3ab2daeb 100644 --- a/src/ElementTrait.php +++ b/src/ElementTrait.php @@ -24,21 +24,20 @@ public function elementAssertAttributeHasValue($selector, $attribute, $value) { throw new \Exception(sprintf('The "%s" element was not found on the page.', $selector)); } - $found = FALSE; $attr_found = FALSE; + $attr_value_found = FALSE; foreach ($elements as $element) { $attr = $element->getAttribute($attribute); if (!empty($attr)) { $attr_found = TRUE; if (strpos($attr, strval($value)) !== FALSE) { - $found = TRUE; + $attr_value_found = TRUE; break; } } - $attr_found = FALSE; } - if (!$found) { + if (!$attr_value_found) { if (!$attr_found) { throw new \Exception(sprintf('The "%s" attribute was not found on the element "%s".', $attribute, $selector)); } diff --git a/src/FileDownloadTrait.php b/src/FileDownloadTrait.php index 85af99d8..0225d7f7 100644 --- a/src/FileDownloadTrait.php +++ b/src/FileDownloadTrait.php @@ -70,7 +70,7 @@ public function fileDownloadFrom($url) { $cookie_list = []; - /** @var Behat\Mink\Driver\BrowserKitDriver $driver */ + /** @var \Behat\Mink\Driver\CoreDriver $driver */ $driver = $this->getSession()->getDriver(); if ($driver instanceof Selenium2Driver) { $cookies = $driver->getWebDriverSession()->getAllCookies(); @@ -79,6 +79,7 @@ public function fileDownloadFrom($url) { } } else { + /** @var \Behat\Mink\Driver\BrowserKitDriver $driver */ $cookies = $driver->getClient()->getCookieJar()->allValues($driver->getCurrentUrl()); foreach ($cookies as $cookie_name => $cookie_value) { $cookie_list[] = $cookie_name . '=' . $cookie_value; @@ -221,11 +222,11 @@ protected function fileDownloadOpenZip() { throw new \RuntimeException('ZIP extension is not enabled for PHP'); } - if (!$this->fileDownloadDownloadedFileInfo || empty($this->fileDownloadDownloadedFileInfo['file_path'])) { + if (empty($this->fileDownloadDownloadedFileInfo) || empty($this->fileDownloadDownloadedFileInfo['file_path'])) { throw new \RuntimeException('Downloaded file path data is not available.'); } - if (!$this->fileDownloadDownloadedFileInfo || empty($this->fileDownloadDownloadedFileInfo['content_type'])) { + if (empty($this->fileDownloadDownloadedFileInfo) || empty($this->fileDownloadDownloadedFileInfo['content_type'])) { throw new \Exception('Downloaded file information does not have content type data.'); } diff --git a/src/FileTrait.php b/src/FileTrait.php index b8317766..0388263f 100644 --- a/src/FileTrait.php +++ b/src/FileTrait.php @@ -6,6 +6,7 @@ use Behat\Behat\Hook\Scope\BeforeScenarioScope; use Behat\Gherkin\Node\TableNode; use Drupal\Core\File\FileSystemInterface; +use Drupal\file\FileInterface; use Symfony\Component\Filesystem\Filesystem; /** @@ -81,7 +82,7 @@ protected function fileCreateManagedSingle($stub) { /** * Create file entity. */ - protected function fileCreateEntity($stub) { + protected function fileCreateEntity($stub): FileInterface { if (empty($stub->path)) { throw new \RuntimeException('"path" property is required'); } @@ -108,13 +109,8 @@ protected function fileCreateEntity($stub) { throw new \RuntimeException('Unable to prepare directory ' . $directory); } } - $entity = \Drupal::service('file.repository')->writeData(file_get_contents($path), $destination, FileSystemInterface::EXISTS_REPLACE); - if (!$entity) { - throw new \RuntimeException('Unable to save managed file ' . $path); - } - - return $entity; + return \Drupal::service('file.repository')->writeData(file_get_contents($path), $destination, FileSystemInterface::EXISTS_REPLACE); } /** @@ -192,7 +188,7 @@ public function fileCreateUnmanaged($uri, $content = 'test') { $dir = \Drupal::service('file_system')->dirname($uri); if (!file_exists($dir)) { - $dir = \Drupal::service('file_system')->dirname($dir, 0770, TRUE); + \Drupal::service('file_system')->dirname($dir); } file_put_contents($uri, $content); diff --git a/src/LinkTrait.php b/src/LinkTrait.php index bdb814d4..79ab0e0f 100644 --- a/src/LinkTrait.php +++ b/src/LinkTrait.php @@ -149,7 +149,6 @@ public function linkClickWithTitle($title) { * @Then the link( with title) :text is an absolute link */ public function assertLinkAbsolute($text) { - /** @var \Behat\Mink\Element\DocumentElement $page */ $link = $this->getSession()->getPage()->findLink($text); if (!$link) { throw new \Exception(sprintf('The link "%s" is not found', $text)); @@ -166,7 +165,6 @@ public function assertLinkAbsolute($text) { * @Then the link( with title) :text is not an absolute link */ public function assertLinkNotAbsolute($text) { - /** @var \Behat\Mink\Element\DocumentElement $page */ $link = $this->getSession()->getPage()->findLink($text); if (!$link) { throw new \Exception(sprintf('The link "%s" is not found', $text)); diff --git a/src/ParagraphsTrait.php b/src/ParagraphsTrait.php index 54adf4e3..40562b63 100644 --- a/src/ParagraphsTrait.php +++ b/src/ParagraphsTrait.php @@ -90,17 +90,16 @@ public function paragraphsAddToEntityWithFields($field_name, $bundle, $entity_ty * Field name on the entity that refers paragraphs item. * @param string $paragraph_bundle * Paragraphs item bundle name. - * @param object $stub + * @param \StdClass $stub * Standard object with filled-in fields. Fields are merged with created * paragraphs item object. * @param bool $save_entity * Flag to save node after attaching a paragraphs item. Defaults to TRUE. * - * @return object - * Create paragraphs item. + * @return \Drupal\paragraphs\Entity\Paragraph + * Created paragraphs item. */ - protected function paragraphsAttachFromStubToEntity(ContentEntityInterface $entity, $entity_field_name, $paragraph_bundle, $stub, $save_entity = TRUE) { - /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */ + protected function paragraphsAttachFromStubToEntity(ContentEntityInterface $entity, string $entity_field_name, string $paragraph_bundle, \StdClass $stub, bool $save_entity = TRUE): Paragraph { $stub->type = $paragraph_bundle; $stub = (array) $stub; $paragraph = Paragraph::create($stub); @@ -166,7 +165,7 @@ protected function paragraphsExpandEntityFields($entity_type, $stub) { * Get a field name that references the paragraphs item. */ protected function paragraphsCheckEntityFieldName($entity_type, $bundle, $field_name) { - /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $entity_field_manager */ + /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $field_info */ $field_info = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type, $bundle); if (!array_key_exists($field_name, $field_info)) { diff --git a/src/ResponseTrait.php b/src/ResponseTrait.php index 387dce6b..cd467a22 100644 --- a/src/ResponseTrait.php +++ b/src/ResponseTrait.php @@ -24,7 +24,7 @@ public function responseAssertContainsHeader($name) { $header = $this->getSession()->getResponseHeader($name); if (!$header) { - throw new \Exception(sprintf('Response does not contain header %s', $name), $this->getSession()->getDriver()); + throw new \Exception(sprintf('Response does not contain header %s', $name)); } } @@ -41,7 +41,7 @@ public function responseAssertNotContainsHeader($name) { $header = $this->getSession()->getResponseHeader($name); if ($header) { - throw new \Exception(sprintf('Response contains header %s, but should not', $name), $this->getSession()->getDriver()); + throw new \Exception(sprintf('Response contains header %s, but should not', $name)); } } diff --git a/src/WatchdogTrait.php b/src/WatchdogTrait.php index 010ec716..4009568c 100644 --- a/src/WatchdogTrait.php +++ b/src/WatchdogTrait.php @@ -101,7 +101,7 @@ public function watchdogAssertErrors(AfterScenarioScope $scope) { $entries = $database->select('watchdog', 'w') ->fields('w') ->condition('w.type', $this->watchdogMessageTypes, 'IN') - ->condition('w.timestamp', $this->watchdogScenarioStartTime, '>=') + ->condition('w.timestamp', (string) $this->watchdogScenarioStartTime, '>=') ->execute() ->fetchAll(); diff --git a/src/WysiwygTrait.php b/src/WysiwygTrait.php index 4adc01fb..fe90375e 100644 --- a/src/WysiwygTrait.php +++ b/src/WysiwygTrait.php @@ -26,12 +26,11 @@ public function wysiwygFillField($field, $value) { $field = $this->wysiwygFixStepArgument($field); $value = $this->wysiwygFixStepArgument($value); - /** @var \Behat\Mink\Element\DocumentElement $page */ $page = $this->getSession()->getPage(); - $field = $page->findField($field); + $element = $page->findField($field); - if ($field === NULL) { - throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value|placeholder', $field); + if ($element === NULL) { + throw new ElementNotFoundException($this->getSession()->getDriver(), 'form field', 'id|name|label|value|placeholder', $field); } $driver = $this->getSession()->getDriver(); @@ -40,17 +39,17 @@ public function wysiwygFillField($field, $value) { } catch (UnsupportedDriverActionException $exception) { // For non-JS drivers process field in a standard way. - $field->setValue($value); + $element->setValue($value); return; } // For a JS-capable driver, try to find WYSIWYG iframe as a child of the // following sibling. - $iframe_xpath = $field->getXpath() . "/following-sibling::div[contains(@class, 'cke')]//iframe"; + $iframe_xpath = $element->getXpath() . "/following-sibling::div[contains(@class, 'cke')]//iframe"; $page_iframe_elements = $driver->find($iframe_xpath); - if (empty($page_iframe_elements) || $page_iframe_elements[0] === NULL) { - throw new ElementNotFoundException($this->getDriver(), 'WYSIWYG form field', 'id|name|label|value|placeholder', $field); + if (empty($page_iframe_elements[0])) { + throw new ElementNotFoundException($this->getSession()->getDriver(), 'WYSIWYG form field', 'id|name|label|value|placeholder', $field); } $iframe_element = reset($page_iframe_elements); @@ -79,7 +78,7 @@ public function wysiwygFillField($field, $value) { } // Select WYSIWYG iframe frame. - $driver->switchToIFrame($index); + $driver->switchToIFrame((string) $index); // Type value as keys into 'body' of iframe. foreach (str_split($value) as $char) { diff --git a/tests/behat/bootstrap/FeatureContextTrait.php b/tests/behat/bootstrap/FeatureContextTrait.php index ccfbeb9f..76bd651a 100644 --- a/tests/behat/bootstrap/FeatureContextTrait.php +++ b/tests/behat/bootstrap/FeatureContextTrait.php @@ -81,7 +81,6 @@ public function assertCookieExists($name) { protected function getCookies() { $cookie_list = []; - /** @var Behat\Mink\Driver\BrowserKitDriver $driver */ $driver = $this->getSession()->getDriver(); if ($driver instanceof Selenium2Driver) { $cookies = $driver->getWebDriverSession()->getAllCookies(); @@ -90,6 +89,7 @@ protected function getCookies() { } } else { + /** @var \Behat\Mink\Driver\BrowserKitDriver $driver */ $cookie_list = $driver->getClient()->getCookieJar()->allValues($driver->getCurrentUrl()); } @@ -161,9 +161,9 @@ public function sendTestEmail($email, PyStringNode $string) { 'mysite_core', 'test_email', $email, - \Drupal::languageManager()->getDefaultLanguage(), + \Drupal::languageManager()->getDefaultLanguage()->getId(), ['body' => strval($string)], - FALSE + NULL ); }