Skip to content

Commit

Permalink
ci: pass tests
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Sep 17, 2023
1 parent 521dc33 commit 38c5244
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 42 deletions.
6 changes: 6 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
colors="true"
cacheDirectory="test/phpunit/.phpunit.cache"
bootstrap="vendor/autoload.php"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
>
<coverage />

Expand Down
82 changes: 48 additions & 34 deletions src/Condition/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,68 @@ public function getLogic():string {

public function getCondition(
string $subLogic = null,
string $separator = "\n\t"
string $separator = "\n\t",
):string {
$condition = "";
$brackets = false;

foreach($this->parts as $i => $part) {
if(strlen($condition) > 0) {
$condition .= $separator;
}
$condition = $this->addPartToCondition(
$i,
$part,
$condition,
$separator,
$subLogic,
);
$brackets = $brackets || $part instanceof Condition;
}

if($part instanceof Condition) {
$logic = $part->getLogic();
if($brackets) {
$condition = "( $condition )";
}

if(strlen($condition) > 0) {
$condition .= $logic;
$condition .= " ";
}
$condition .= $part->getCondition($logic);
$brackets = true;
continue;
}
elseif(is_string($part)) {
if($part[0] === "?") {
$part = substr($part, 1) . " = ?";
}
elseif($part[0] === ":") {
$part = substr($part, 1) . " = $part";
}
}
return $condition;
}

if($i > 0) {
if($subLogic) {
$condition .= $subLogic;
}
else {
$condition .= $this->getLogic();
}
private function addPartToCondition(
int $i,
string|Condition $part,
string $condition,
string $separator,
string $subLogic = null,
):string {
if(strlen($condition) > 0) {
$condition .= $separator;
}

$condition .= " ";
if($part instanceof Condition) {
$logic = $part->getLogic();
if(strlen($condition) > 0) {
$condition .= "$logic ";
}
$condition .= $part->getCondition($logic);
}
else {
$part = $this->processStringPart($part);
if($i > 0) {
$subLogic = $subLogic ?: $this->getLogic();
$condition .= "$subLogic ";
}
$condition .= $part;
}

if($brackets) {
$condition = "( $condition )";
return $condition;
}

private function processStringPart(string $part):string {
if($part[0] === "?") {
$part = substr($part, 1) . " = ?";
}
elseif($part[0] === ":") {
$part = substr($part, 1) . " = $part";
}

return $condition;
return $part;
}

public function getShortParameterSyntax():?string {
Expand All @@ -75,7 +89,7 @@ public function getShortParameterSyntax():?string {

$char1 = $part[0];
if($char1 === "?"
|| $char1 === ":") {
|| $char1 === ":") {
return $char1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Query/ReplaceQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function select():?SelectQuery {
protected function normaliseSet(array $setData):array {
$normalised = [];

if(is_array($setData[0])) {
if(isset($setData[0]) && is_array($setData[0])) {
$setData = $setData[0];
}

Expand Down
1 change: 1 addition & 0 deletions src/Query/SelectQuery.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Gt\SqlBuilder\Query;

/** @SuppressWarnings(PHPMD.TooManyPublicMethods) */
abstract class SelectQuery extends SqlQuery {
public function __construct(
protected bool $subQuery = false
Expand Down
19 changes: 12 additions & 7 deletions src/Query/SqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Gt\SqlBuilder\Condition\MixedIndexedAndNamedParametersException;
use Stringable;

/** @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */
abstract class SqlQuery implements Stringable {
const PRE_QUERY_COMMENT = "/* preQuery */";
const POST_QUERY_COMMENT = "/* postQuery */";
Expand All @@ -29,7 +30,11 @@ public function postQuery():string {
return "";
}

/** @param array<string, string>|array<string, string[]|mixed> $clauses */
/**
* @param array<string, string>|array<string, string[]|mixed> $clauses
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @phpcs:disable Generic.Metrics.CyclomaticComplexity
*/
protected function processClauseList(array $clauses):string {
$query = "";

Expand Down Expand Up @@ -59,33 +64,33 @@ protected function processClauseList(array $clauses):string {
elseif($name === "partition") {
$query .= $this->processPartitionClause($parts);
}
elseif(strstr($name, "rowSelect")) {
elseif(str_contains($name, "rowSelect")) {
if(isset($parts[0]) && $parts[0] instanceof SelectQuery) {
$query .= $parts[0];
}
}
elseif(strstr($name, "join")) {
elseif(str_contains($name, "join")) {
$query .= $this->processJoinClause(
$name,
$parts
);
}
elseif(strstr($name, "limit")) {
elseif(str_contains($name, "limit")) {
if(isset($parts[0])) {
$query .= "\nlimit $parts[0]";
}
}
elseif(strstr($name, "offset")) {
elseif(str_contains($name, "offset")) {
if(isset($parts[0])) {
$query .= "\noffset $parts[0]";
}
}
elseif(strstr($name, "create definition")) {
elseif(str_contains($name, "create definition")) {
$query .= "(";
$query .= implode(", \n", $parts);
$query .= ")";
}
elseif(strstr($name, "alter options")) {
elseif(str_contains($name, "alter options")) {
$query .= implode(", \n", $parts);
}
else {
Expand Down

0 comments on commit 38c5244

Please sign in to comment.