Skip to content

Commit

Permalink
SW-27049 - Update PHPStan and improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
mitelg committed Aug 21, 2023
1 parent 0041829 commit 1c85174
Show file tree
Hide file tree
Showing 72 changed files with 600 additions and 1,425 deletions.
1 change: 1 addition & 0 deletions tests/.php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

'class_attributes_separation' => ['elements' => ['method' => 'one', 'property' => 'one']],
'concat_space' => ['spacing' => 'one'],
'declare_strict_types' => true,
'doctrine_annotation_indentation' => true,
'doctrine_annotation_spaces' => true,
'general_phpdoc_annotation_remove' => [
Expand Down
10 changes: 4 additions & 6 deletions tests/Component/Api/ApiClient.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Shopware\Component\Api;

use Faker\Factory as FakerFactory;
Expand Down Expand Up @@ -119,17 +121,13 @@ public function delete($url, $params = [])
}

/**
* @param string $name
*
* @throws \Exception
*
* @return bool
*/
public function articleExistsByName($name)
public function articleExistsByName(string $name): bool
{
$response = $this->get('api/articles', [
'filter' => [
'name' => (string) $name,
'name' => $name,
],
'limit' => 1,
]);
Expand Down
20 changes: 7 additions & 13 deletions tests/Component/Form/FormFillerTrait.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

declare(strict_types=1);

namespace Shopware\Component\Form;

use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\ElementNotFoundException;
use Shopware\Component\XpathBuilder\FrontendXpathBuilder;
use Shopware\Page\ContextAwarePage;
Expand All @@ -11,9 +14,9 @@ trait FormFillerTrait
/**
* Fill a given form where the fields are identified by their name and tag types
*
* @param array $formData Expected format: [['name' => 'sAGB', 'value' => '1', 'type' => 'checkbox']]
* @param array<array{name: string, value: mixed, type: string}> $formData Expected format: [['name' => 'sAGB', 'value' => '1', 'type' => 'checkbox']]
*/
public function fillForm(ContextAwarePage $page, array $formData)
public function fillForm(ContextAwarePage $page, array $formData): void
{
foreach ($formData as $formElement) {
switch ($formElement['type']) {
Expand All @@ -35,12 +38,8 @@ public function fillForm(ContextAwarePage $page, array $formData)

/**
* Get a NodeElement with the given name
*
* @param string $xpath
*
* @return \Behat\Mink\Element\NodeElement|null
*/
private function getElementByName(ContextAwarePage $page, $xpath)
private function getElementByName(ContextAwarePage $page, string $xpath): NodeElement
{
return $page->waitForSelectorPresent('xpath', $xpath);
}
Expand All @@ -57,12 +56,7 @@ private function isCheckboxChecked(ContextAwarePage $page, string $inputName): b
}
}

/**
* @param string $xpath
*
* @return string
*/
private function selectLastElement($xpath)
private function selectLastElement(string $xpath): string
{
return sprintf('(%s)[last()]', $xpath);
}
Expand Down
16 changes: 7 additions & 9 deletions tests/Component/SpinTrait/SpinTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Shopware\Component\SpinTrait;

trait SpinTrait
Expand All @@ -9,16 +11,14 @@ trait SpinTrait
*
* @see http://docs.behat.org/en/v2.5/cookbook/using_spin_functions.html#adding-a-timeout
*
* @param int $wait
* @param callable $lambda
*
* @throws \Exception
*
* @return bool
*/
protected function spin($lambda, $wait = 120)
protected function spin($lambda, int $wait = 10): void
{
if (!$this->spinWithNoException($lambda, $wait)) {
throw new \Exception("Spin function timed out after {$wait} seconds");
throw new \Exception(sprintf('Spin function timed out after %s seconds', $wait));
}
}

Expand All @@ -27,11 +27,9 @@ protected function spin($lambda, $wait = 120)
*
* @see http://docs.behat.org/en/v2.5/cookbook/using_spin_functions.html#adding-a-timeout
*
* @param int $wait
*
* @return bool
* @param callable $lambda
*/
protected function spinWithNoException($lambda, $wait = 120)
protected function spinWithNoException($lambda, int $wait = 10): bool
{
$time = time();
$stopTime = $time + $wait;
Expand Down
82 changes: 13 additions & 69 deletions tests/Component/XpathBuilder/BackendXpathBuilder.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Shopware\Component\XpathBuilder;

class BackendXpathBuilder extends BaseXpathBuilder
Expand All @@ -9,13 +11,8 @@ class BackendXpathBuilder extends BaseXpathBuilder
*
* This function builds an xpath that matches the window name exactly, but
* allows explicit fuzziness by passing in 'false' as the second parameter.
*
* @param string $title
* @param bool $exactMatch
*
* @return string
*/
public static function getWindowXpathByTitle($title, $exactMatch = true)
public static function getWindowXpathByTitle(string $title, bool $exactMatch = true): string
{
$prefix = $exactMatch ? '@' : '~';

Expand All @@ -27,14 +24,8 @@ public static function getWindowXpathByTitle($title, $exactMatch = true)

/**
* Get an xpath for an extJS form element by its label
*
* @param string $label
* @param string $tag
* @param string $scope
*
* @return string
*/
public static function getFormElementXpathByLabel($label, $tag, $scope = '/')
public static function getFormElementXpathByLabel(string $label, string $tag, string $scope = '/'): string
{
return static::create($scope)
->descendant('label', ['@text' => $label])
Expand All @@ -46,13 +37,8 @@ public static function getFormElementXpathByLabel($label, $tag, $scope = '/')

/**
* Return an Xpath that finds a button by its label
*
* @param string $label
* @param string $scope
*
* @return string
*/
public static function getButtonXpathByLabel($label, $scope = '/')
public static function getButtonXpathByLabel(string $label, string $scope = '/'): string
{
return static::create($scope)
->child('span', ['@class' => 'x-btn-inner'])
Expand All @@ -63,26 +49,16 @@ public static function getButtonXpathByLabel($label, $scope = '/')

/**
* Shorthand function to get an extJS input field by its label
*
* @param string $label
* @param string $scope
*
* @return string
*/
public static function getInputXpathByLabel($label, $scope = '/')
public static function getInputXpathByLabel(string $label, string $scope = '/'): string
{
return self::getFormElementXpathByLabel($label, 'input', $scope);
}

/**
* Returns label-specific xpath for a combobox
*
* @param string $label
* @param string $scope
*
* @return string
*/
public static function getComboboxXpathByLabel($label, $scope = '/')
public static function getComboboxXpathByLabel(string $label, string $scope = '/'): string
{
return static::create($scope)
->descendant('label', ['@text' => $label])
Expand All @@ -95,22 +71,16 @@ public static function getComboboxXpathByLabel($label, $scope = '/')

/**
* Return xpath to the currently focused extJs input
*
* @return string
*/
public static function getFocusedElementXpath()
public static function getFocusedElementXpath(): string
{
return (new self())->child('input', ['~class' => 'x-form-focus'])->getXpath();
}

/**
* Return xpath to an extJs tab by its label
*
* @param string $label
*
* @return string
*/
public static function getTabXpathByLabel($label)
public static function getTabXpathByLabel(string $label): string
{
return (new self())
->child('span', ['@text' => $label])
Expand All @@ -121,13 +91,9 @@ public static function getTabXpathByLabel($label)
/**
* Return xpath to extJs icon by type
*
* @param string $type
*
* @throws \Exception
*
* @return string
*@throws \Exception
*/
public static function getIconXpathByType($type)
public static function getIconXpathByType(string $type): string
{
switch ($type) {
case 'edit':
Expand All @@ -137,36 +103,14 @@ public static function getIconXpathByType($type)
return (new self())->child('img', ['~class' => 'sprite-minus-circle-frame'])->getXpath();
break;
default:
throw new \Exception('Unknown icon type ' . $type);
throw new \RuntimeException('Unknown icon type ' . $type);
}
}

/**
* Return a dropdown xpath by its action
*
* @param string $action
* @param string $optionText
*
* @return string
*/
public function getDropdownXpathByAction($action, $optionText = '')
{
$this->child('div', ['~class' => 'x-boundlist', 'and', '@data-action' => $action]);

return empty($optionText)
? $this->descendant('li', ['@role' => 'option'])->getXpath()
: $this->descendant('li', ['@role' => 'option', 'and', '@text' => $optionText])->getXpath();
}

/**
* Return an Xpath that finds a fieldset by its label
*
* @param string $label
* @param string $scope
*
* @return string
*/
public static function getFieldsetXpathByLabel($label, $scope = '/')
public static function getFieldsetXpathByLabel(string $label, string $scope = '/'): string
{
return static::create($scope)
->descendant('fieldset')
Expand Down
Loading

0 comments on commit 1c85174

Please sign in to comment.