Skip to content

Commit

Permalink
Fixed several static errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Oct 15, 2023
1 parent bfd7076 commit 2e194ce
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 46 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

parameters:

level: 1
level: 5

paths:
- ../src
Expand Down
2 changes: 1 addition & 1 deletion src/BigPipeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/DateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}

Expand Down
10 changes: 5 additions & 5 deletions src/DraggableViewsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/ElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
7 changes: 4 additions & 3 deletions src/FileDownloadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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.');
}

Expand Down
12 changes: 4 additions & 8 deletions src/FileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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');
}
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions src/LinkTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand Down
11 changes: 5 additions & 6 deletions src/ParagraphsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/WatchdogTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
17 changes: 8 additions & 9 deletions src/WysiwygTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions tests/behat/bootstrap/FeatureContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -90,6 +89,7 @@ protected function getCookies() {
}
}
else {
/** @var \Behat\Mink\Driver\BrowserKitDriver $driver */
$cookie_list = $driver->getClient()->getCookieJar()->allValues($driver->getCurrentUrl());
}

Expand Down Expand Up @@ -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
);
}

Expand Down

0 comments on commit 2e194ce

Please sign in to comment.