Skip to content

Commit

Permalink
improve readability when extracting from value/result
Browse files Browse the repository at this point in the history
  • Loading branch information
robocoder committed Aug 9, 2022
1 parent 6df9fe9 commit ed8f774
Showing 1 changed file with 50 additions and 45 deletions.
95 changes: 50 additions & 45 deletions lib/WebDriver/AbstractWebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
$url .= '/' . $parameters;
}

$this->assertNonObjectParameters($parameters);
$this->assertSerializable($parameters);

list($rawResult, $info) = ServiceFactory::getInstance()->getService('service.curl')->execute($requestMethod, $url, $parameters, $extraOptions);

Expand All @@ -135,8 +135,8 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti

$result = json_decode($rawResult, true);

if (!empty($rawResult) && $result === null && json_last_error() != JSON_ERROR_NONE) {
// Legacy webdriver 4xx responses are to be considered // an error and return plaintext
if (! empty($rawResult) && $result === null && json_last_error() != JSON_ERROR_NONE) {
// Legacy webdriver 4xx responses are to be considered a plaintext error
if ($httpCode >= 400 && $httpCode <= 499) {
throw WebDriverException::factory(
WebDriverException::CURL_EXEC,
Expand All @@ -157,13 +157,9 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
);
}

$value = (is_array($result) && array_key_exists('value', $result)) ? $result['value'] : null;
$message = (is_array($result) && array_key_exists('message', $result))
? $result['message']
: ((is_array($value) && array_key_exists('message', $value)) ? $value['message'] : null);
$error = (is_array($result) && array_key_exists('error', $result))
? $result['error']
: ((is_array($value) && array_key_exists('error', $value)) ? $value['error'] : null);
$value = $this->offsetGet('value', $result);
$message = $this->offsetGet('message', $result) ?: $this->offsetGet('message', $value);
$error = $this->offsetGet('error', $result) ?: $this->offsetGet('error', $value);

// if not success, throw exception
if (isset($result['status']) && (int) $result['status'] !== 0) {
Expand All @@ -180,15 +176,9 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
);
}

$sessionId = isset($result['sessionId'])
? $result['sessionId']
: (isset($value['sessionId'])
? $value['sessionId']
: (isset($value['webdriver.remote.sessionid'])
? $value['webdriver.remote.sessionid']
: null
)
);
$sessionId = $this->offsetGet('sessionId', $result)
?: $this->offsetGet('sessionId', $value)
?: $this->offsetGet('webdriver.remote.sessionid', $value);

return array(
'value' => $value,
Expand All @@ -198,32 +188,6 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
);
}

/**
* @param mixed $parameters
*/
private function assertNonObjectParameters($parameters)
{
if ($parameters === null || is_scalar($parameters)) {
return;
}

if (is_array($parameters)) {
foreach ($parameters as $value) {
$this->assertNonObjectParameters($value);
}

return;
}

throw WebDriverException::factory(
WebDriverException::UNEXPECTED_PARAMETERS,
sprintf(
"Unable to serialize non-scalar type %s",
is_object($parameters) ? get_class($parameters) : gettype($parameters)
)
);
}

/**
* Magic method that maps calls to class methods to execute WebDriver commands
*
Expand Down Expand Up @@ -273,6 +237,47 @@ public function __call($name, $arguments)
return $result['value'];
}

/**
* Sanity check
*
* @param mixed $parameters
*/
private function assertSerializable($parameters)
{
if ($parameters === null || is_scalar($parameters)) {
return;
}

if (is_array($parameters)) {
foreach ($parameters as $value) {
$this->assertSerializable($value);
}

return;
}

throw WebDriverException::factory(
WebDriverException::UNEXPECTED_PARAMETERS,
sprintf(
"Unable to serialize non-scalar type %s",
is_object($parameters) ? get_class($parameters) : gettype($parameters)
)
);
}

/**
* Extract value from result
*
* @param string $key
* @param mixed $result
*
* @return string|null
*/
private function offsetGet($key, $result)
{
return (is_array($result) && array_key_exists($key, $result)) ? $result[$key] : null;
}

/**
* Get default HTTP request method for a given WebDriver command
*
Expand Down

0 comments on commit ed8f774

Please sign in to comment.