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

MDTT-38: Fix for CI failure. #24

Merged
merged 11 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
16 changes: 9 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ jobs:
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader

- name: Security check installed dependencies
uses: symfonycorp/security-checker-action@v2
uses: symfonycorp/security-checker-action@v4

- name: Check PSR2 code style (PHP_CodeSniffer)
run: composer php-cs

- name: Analyse PHP Code (PHPStan)
run: composer phpstan

- name: Validate code quality
run: composer phpunit-with-coverage
# As codecov throwing lot of errors, disabling the respective check temporarily
# Reference discussion: https://github.com/axelerant/mdtt/pull/24#pullrequestreview-1632092133
# - name: Validate code quality
# run: composer phpunit-with-coverage

- name: Send code coverage report to Codecov.io
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
# - name: Send code coverage report to Codecov.io
# uses: codecov/codecov-action@v2
# with:
# token: ${{ secrets.CODECOV_TOKEN }}
10 changes: 10 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Mdtt\Exception;

class InvalidArgumentException extends \Exception
{

}
sadeesh-sdet marked this conversation as resolved.
Show resolved Hide resolved
64 changes: 58 additions & 6 deletions src/Test/DefaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Mdtt\Exception\ExecutionException;
use PHPUnit\Framework\Assert;
use Mdtt\Exception\InvalidArgumentException;

class DefaultTest extends Test
{

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -53,20 +53,72 @@ public function execute(array $sourceData, array $destinationData): bool
}

/**
* @param array<string, numeric-string|array<string, numeric-string>>|numeric-string $data
* @param array<string> $fields
* Recursively checks if an offset exists in a nested array.
*
* This function is designed to handle nested arrays and verify the existence of
* a specified offset (string key) within them. It performs recursive checks on
* nested arrays to ensure that the offset exists at each level.
*
* @param array<string, numeric-string|array<string, numeric-string>> $data
* The data array to check for the offset in.
* @param array<string> $fields
* An array of string keys representing the path to the desired offset.
*
* @return bool
* Returns `true` if the specified offset exists in the nested array,
* `false` otherwise.
*
* @throws InvalidArgumentException
* If the input data structure is not as expected.
*/
private function issetField(mixed $data, array $fields): bool
private function issetField(array $data, array $fields): bool
{
// Get the next key to check
$key = array_shift($fields);

// If there are no more keys to check, the offset exists
if ($key === null) {
return true;
}

$test = isset($data[$key]);
// Check if the key exists in the data array
if (!array_key_exists($key, $data)) {
sadeesh-sdet marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

// Make sure that the key value is an array of strings
if (!is_array($data[$key]) || !$this->isListOfStrings($data[$key])) {
throw new InvalidArgumentException("Data structure is not as expected.");
}

// Recursively check the next level
return $this->issetField($data[$key], $fields);
}

/**
* Checks if an array contains a list of strings.
*
* @param iterable<string> $value
* The array to check.
*
* @return bool
* Returns `true` if the array contains a list of strings, `false` otherwise.
*
* @throws InvalidArgumentException
* If the input value is not an array.
*/
private function isListOfStrings(iterable $value): bool
sadeesh-sdet marked this conversation as resolved.
Show resolved Hide resolved
{
if (!is_array($value)) {
throw new InvalidArgumentException("Input must be an array.");
}

foreach ($value as $item) {
if (!is_string($item)) {
return false;
}
}

return $test && $this->issetField($data[$key], $fields);
return true;
}
}
Loading