Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH PHP 8.1 compatibility #212

Merged
merged 1 commit into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Compiler/CoreInitializationPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function process(ContainerBuilder $container)
$container->setParameter('paths.modules.'.$module->getShortName(), $module->getPath());
$composerName = $module->getComposerName();
if ($composerName) {
list($vendor,$name) = explode('/', $composerName);
list($vendor,$name) = explode('/', $composerName ?? '');
$container->setParameter('paths.modules.'.$vendor.'.'.$name, $module->getPath());
}
}
Expand Down
86 changes: 43 additions & 43 deletions src/Context/BasicContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private function getSubclassesOf($parent): array
{
$result = [];
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, $parent)) {
if (is_subclass_of($class, $parent ?? '')) {
$result[] = $class;
}
}
Expand Down Expand Up @@ -211,9 +211,9 @@ public function handleAjaxBeforeStep(BeforeStepScope $event)
}
try {
$ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps();
$ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps));
$ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps ?? []));

if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) {
if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText() ?? '')) {
return;
}

Expand Down Expand Up @@ -264,9 +264,9 @@ public function handleAjaxAfterStep(AfterStepScope $event)
}
try {
$ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps();
$ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps));
$ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps ?? []));

if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) {
if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText() ?? '')) {
return;
}

Expand Down Expand Up @@ -377,7 +377,7 @@ protected function findNamedButton($title)
{
$page = $this->getSession()->getPage();
// See https://mathiasbynens.be/notes/css-escapes
$escapedTitle = addcslashes($title, '!"#$%&\'()*+,-./:;<=>?@[\]^`{|}~');
$escapedTitle = addcslashes($title ?? '', '!"#$%&\'()*+,-./:;<=>?@[\]^`{|}~');
$matchedEl = null;
$searches = [
['named', ['link_or_button', "'{$title}'"]],
Expand Down Expand Up @@ -406,7 +406,7 @@ protected function findNamedButton($title)
public function iShouldSeeAButton($negative, $text)
{
$button = $this->findNamedButton($text);
if (trim($negative)) {
if (trim($negative ?? '')) {
Assert::assertNull($button, sprintf('%s button found', $text));
} else {
Assert::assertNotNull($button, sprintf('%s button not found', $text));
Expand All @@ -430,9 +430,9 @@ public function stepIPressTheButton($text)
*/
public function stepIPressTheButtons($text)
{
$buttonNames = explode('|', $text);
$buttonNames = explode('|', $text ?? '');
foreach ($buttonNames as $name) {
$button = $this->findNamedButton(trim($name));
$button = $this->findNamedButton(trim($name ?? ''));
if ($button) {
break;
}
Expand Down Expand Up @@ -718,14 +718,14 @@ public function iPutABreakpoint()
*/
public function castRelativeToAbsoluteTime($prefix, $val)
{
$timestamp = strtotime($val);
$timestamp = strtotime($val ?? '');
if (!$timestamp) {
throw new InvalidArgumentException(sprintf(
"Can't resolve '%s' into a valid datetime value",
$val
));
}
return date($this->timeFormat, $timestamp);
return date($this->timeFormat ?? '', $timestamp);
}

/**
Expand All @@ -740,14 +740,14 @@ public function castRelativeToAbsoluteTime($prefix, $val)
*/
public function castRelativeToAbsoluteDatetime($prefix, $val)
{
$timestamp = strtotime($val);
$timestamp = strtotime($val ?? '');
if (!$timestamp) {
throw new InvalidArgumentException(sprintf(
"Can't resolve '%s' into a valid datetime value",
$val
));
}
return date($this->datetimeFormat, $timestamp);
return date($this->datetimeFormat ?? '', $timestamp);
}

/**
Expand All @@ -762,14 +762,14 @@ public function castRelativeToAbsoluteDatetime($prefix, $val)
*/
public function castRelativeToAbsoluteDate($prefix, $val)
{
$timestamp = strtotime($val);
$timestamp = strtotime($val ?? '');
if (!$timestamp) {
throw new InvalidArgumentException(sprintf(
"Can't resolve '%s' into a valid datetime value",
$val
));
}
return date($this->dateFormat, $timestamp);
return date($this->dateFormat ?? '', $timestamp);
}

public function getDateFormat()
Expand Down Expand Up @@ -828,7 +828,7 @@ public function stepFieldShouldBeDisabled($name, $type, $negate)
Assert::assertNotNull($element, sprintf("Element '%s' not found", $name));

$disabledAttribute = $element->getAttribute('disabled');
if (trim($negate)) {
if (trim($negate ?? '')) {
Assert::assertNull($disabledAttribute, sprintf("Failed asserting element '%s' is not disabled", $name));
} else {
Assert::assertNotNull($disabledAttribute, sprintf("Failed asserting element '%s' is disabled", $name));
Expand Down Expand Up @@ -929,11 +929,11 @@ public function iSeeTextInRegion($negate, $text, $region)
Assert::assertNotNull($regionObj);

$actual = $regionObj->getText();
$actual = preg_replace('/\s+/u', ' ', $actual);
$regex = '/' . preg_quote($text, '/') . '/ui';
$actual = preg_replace('/\s+/u', ' ', $actual ?? '');
$regex = '/' . preg_quote($text ?? '', '/') . '/ui';

if (trim($negate)) {
if (preg_match($regex, $actual)) {
if (trim($negate ?? '')) {
if (preg_match($regex ?? '', $actual ?? '')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (preg_match($regex ?? '', $actual ?? '')) {
if (preg_match($regex ?? '//', $actual ?? '')) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was discussed in another PR - the warning emitted by the empty string is correct and we would be hiding an actual issue by using an empty regex. The intention here is just to avoid deprecation warnings.

$message = sprintf(
'The text "%s" was found in the text of the "%s" region on the page %s.',
$text,
Expand All @@ -944,7 +944,7 @@ public function iSeeTextInRegion($negate, $text, $region)
throw new \Exception($message);
}
} else {
if (!preg_match($regex, $actual)) {
if (!preg_match($regex ?? '', $actual ?? '')) {
$message = sprintf(
'The text "%s" was not found anywhere in the text of the "%s" region on the page %s.',
$text,
Expand Down Expand Up @@ -1079,15 +1079,15 @@ public function theTextBeforeAfter($textBefore, $order, $textAfter, $element)

// Check both of the texts exist in the element
$text = $ele->getText();
Assert::assertTrue(strpos($text, $textBefore) !== 'FALSE', sprintf('%s not found in the element %s', $textBefore, $element));
Assert::assertTrue(strpos($text, $textAfter) !== 'FALSE', sprintf('%s not found in the element %s', $textAfter, $element));
Assert::assertTrue(strpos($text ?? '', $textBefore ?? '') !== 'FALSE', sprintf('%s not found in the element %s', $textBefore, $element));
Assert::assertTrue(strpos($text ?? '', $textAfter ?? '') !== 'FALSE', sprintf('%s not found in the element %s', $textAfter, $element));

/// Use strpos to get the position of the first occurrence of the two texts (case-sensitive)
// and compare them with the given order (before or after)
if ($order === 'before') {
Assert::assertTrue(strpos($text, $textBefore) < strpos($text, $textAfter));
Assert::assertTrue(strpos($text ?? '', $textBefore ?? '') < strpos($text ?? '', $textAfter ?? ''));
} else {
Assert::assertTrue(strpos($text, $textBefore) > strpos($text, $textAfter));
Assert::assertTrue(strpos($text ?? '', $textBefore ?? '') > strpos($text ?? '', $textAfter ?? ''));
}
}

Expand Down Expand Up @@ -1302,7 +1302,7 @@ public function iShouldSeeTheElement($not, $cssSelector = '')
$not = '';
$cssSelector = $not;
}
$sel = str_replace('"', '\\"', $cssSelector);
$sel = str_replace('"', '\\"', $cssSelector ?? '');
$js = <<<JS
return document.querySelector("$sel");
JS;
Expand Down Expand Up @@ -1330,8 +1330,8 @@ public function iSelectFromTheField($value, $locator, $withJavascript)
$field->selectOption($value);
} else {
$xpath = $field->getXpath();
$xpath = str_replace(['"', "\n"], ['\"', ''], $xpath);
$value = str_replace('"', '\"', $value);
$xpath = str_replace(['"', "\n"], ['\"', ''], $xpath ?? '');
$value = str_replace('"', '\"', $value ?? '');
$js = <<<JS
return (function() {
let select = document.evaluate("{$xpath}", document).iterateNext();
Expand Down Expand Up @@ -1360,8 +1360,8 @@ public function iSelectFromTheField($value, $locator, $withJavascript)
public function theRenderedHtmlShouldContain($not, $htmlFragment)
{
$html = $this->getSession()->getPage()->getOuterHtml();
$htmlFragment = str_replace('\"', '"', $htmlFragment);
$contains = strpos($html, $htmlFragment) !== false;
$htmlFragment = str_replace('\"', '"', $htmlFragment ?? '');
$contains = strpos($html ?? '', $htmlFragment ?? '') !== false;
if ($not) {
Assert::assertFalse($contains, "HTML fragment {$htmlFragment} was in rendered HTML when it should not have been");
} else {
Expand Down Expand Up @@ -1463,18 +1463,18 @@ public function iPressTheKeyGlobally($keyCombo)
return;
}
$modifier = null;
$pos = strpos($keyCombo, '-');
$pos = strpos($keyCombo ?? '', '-');
if ($pos !== false && $pos !== 0) {
list($modifier, $char) = explode('-', $keyCombo);
list($modifier, $char) = explode('-', $keyCombo ?? '');
} else {
$char = $keyCombo;
}
// handle special chars e.g. "space"
if (defined(WebDriverKeys::class . '::' . strtoupper($char))) {
$char = constant(WebDriverKeys::class . '::' . strtoupper($char));
if (defined(WebDriverKeys::class . '::' . strtoupper($char ?? ''))) {
$char = constant(WebDriverKeys::class . '::' . strtoupper($char ?? ''));
}
if ($modifier) {
$modifier = strtoupper($modifier);
$modifier = strtoupper($modifier ?? '');
if (defined(WebDriverKeys::class . '::' . $modifier)) {
$modifier = constant(WebDriverKeys::class . '::' . $modifier);
} else {
Expand All @@ -1495,13 +1495,13 @@ public function iAttachTheFileToTheField($filename, $locator)
{
Assert::assertNotNull($this->fixtureContext, 'FixtureContext was not found so cannot know location of fixture files');
$path = $this->fixtureContext->getFilesPath() . '/' . $filename;
$path = str_replace('//', '/', $path);
$path = str_replace('//', '/', $path ?? '');
Assert::assertNotEmpty($path, 'Fixture files path is empty');
$field = $this->getElement($locator);
$filesPath = $this->fixtureContext->getFilesPath();
if ($filesPath) {
$fullPath = rtrim(realpath($filesPath), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $path;
if (is_file($fullPath)) {
$fullPath = rtrim(realpath($filesPath ?? '') ?? '', DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $path;
if (is_file($fullPath ?? '')) {
$path = $fullPath;
}
}
Expand All @@ -1526,12 +1526,12 @@ public function iFollowWithJavascript($locator)
}
Assert::assertNotNull($link, "Link {$locator} was not found");
$html = $link->getOuterHtml();
preg_match('#href=([\'"])#', $html, $m);
preg_match('#href=([\'"])#', $html ?? '', $m);
$q = $m[1];
preg_match("#href={$q}(.+?){$q}#", $html, $m);
$href = str_replace("'", "\\'", $m[1]);
if (strpos($href, 'http') !== 0) {
$href = rtrim($href, '/');
preg_match("#href={$q}(.+?){$q}#", $html ?? '', $m);
$href = str_replace("'", "\\'", $m[1] ?? '');
if (strpos($href ?? '', 'http') !== 0) {
$href = rtrim($href ?? '', '/');
$href = "/{$href}";
}
$this->getSession()->executeScript("document.location.href = '{$href}';");
Expand Down
24 changes: 12 additions & 12 deletions src/Context/EmailContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function thereIsAnEmailFromTo($negate, $direction, $email)
$to = ($direction == 'to') ? $email : null;
$from = ($direction == 'from') ? $email : null;
$match = $this->mailer->findEmail($to, $from);
if (trim($negate)) {
if (trim($negate ?? '')) {
Assert::assertNull($match);
} else {
Assert::assertNotNull($match);
Expand All @@ -90,7 +90,7 @@ public function thereIsAnEmailFromToTitled($negate, $direction, $email, $subject
$allTitles = $allMails ? '"' . implode('","', array_map(function ($email) {
return $email->Subject;
}, $allMails)) . '"' : null;
if (trim($negate)) {
if (trim($negate ?? '')) {
Assert::assertNull($match);
} else {
$msg = sprintf(
Expand Down Expand Up @@ -130,7 +130,7 @@ public function thereTheEmailContains($negate, $content)
$emailContent = $email->PlainContent;
}

if (trim($negate)) {
if (trim($negate ?? '')) {
Assert::assertStringNotContainsString($content, $emailContent);
} else {
Assert::assertStringContainsString($content, $emailContent);
Expand All @@ -154,8 +154,8 @@ public function thereTheEmailContainsPlainText($content)

$email = $this->lastMatchedEmail;
$emailContent = ($email->Content) ? ($email->Content) : ($email->PlainContent);
$emailPlainText = strip_tags($emailContent);
$emailPlainText = preg_replace("/\h+/", " ", $emailPlainText);
$emailPlainText = strip_tags($emailContent ?? '');
$emailPlainText = preg_replace("/\h+/", " ", $emailPlainText ?? '');

Assert::assertStringContainsString($content, $emailPlainText);
}
Expand Down Expand Up @@ -259,12 +259,12 @@ public function theEmailContainFollowingData($negate, TableNode $table)
$emailContent = $email->PlainContent;
}
// Convert html content to plain text
$emailContent = strip_tags($emailContent);
$emailContent = preg_replace("/\h+/", " ", $emailContent);
$emailContent = strip_tags($emailContent ?? '');
$emailContent = preg_replace("/\h+/", " ", $emailContent ?? '');
$rows = $table->getRows();

// For "should not contain"
if (trim($negate)) {
if (trim($negate ?? '')) {
foreach ($rows as $row) {
Assert::assertStringNotContainsString($row[0], $emailContent);
}
Expand All @@ -283,7 +283,7 @@ public function theEmailContainFollowingData($negate, TableNode $table)
public function thereIsAnEmailTitled($negate, $subject)
{
$match = $this->mailer->findEmail(null, null, $subject);
if (trim($negate)) {
if (trim($negate ?? '')) {
Assert::assertNull($match);
} else {
$msg = sprintf(
Expand All @@ -307,7 +307,7 @@ public function theEmailSentFrom($negate, $from)
}

$match = $this->lastMatchedEmail;
if (trim($negate)) {
if (trim($negate ?? '')) {
Assert::assertStringNotContainsString($from, $match->From);
} else {
Assert::assertStringContainsString($from, $match->From);
Expand All @@ -326,7 +326,7 @@ public function theEmailSentTo($negate, $to)
}

$match = $this->lastMatchedEmail;
if (trim($negate)) {
if (trim($negate ?? '')) {
Assert::assertStringNotContainsString($to, $match->To);
} else {
Assert::assertStringContainsString($to, $match->To);
Expand Down Expand Up @@ -355,7 +355,7 @@ public function iClickOnHttpLinkInEmail($httpText)
$href = null;
foreach ($tags as $tag) {
$linkText = $tag->nodeValue;
if (strpos($linkText, $httpText) !== false) {
if (strpos($linkText ?? '', $httpText ?? '') !== false) {
$href = $linkText;
break;
}
Expand Down
Loading