Skip to content

Commit

Permalink
Update phpstan/phpstan requirement from ^1.12 to ^2.0 in the major-up…
Browse files Browse the repository at this point in the history
…dates group (#10)

* Update phpstan/phpstan requirement || ^2.0 in the major-updates group

Updates the requirements on [phpstan/phpstan](https://github.com/phpstan/phpstan) to permit the latest version.

Updates `phpstan/phpstan` to 2.0.0
- [Release notes](https://github.com/phpstan/phpstan/releases)
- [Changelog](https://github.com/phpstan/phpstan/blob/2.0.x/CHANGELOG.md)
- [Commits](phpstan/phpstan@1.12.0...2.0.0)

---
updated-dependencies:
- dependency-name: phpstan/phpstan
  dependency-type: direct:development
  dependency-group: major-updates
...

Signed-off-by: dependabot[bot] <[email protected]>

* Update dependencies and improve type handling in results

Updated `phpstan/phpstan` to the required version 2.0 in composer.json. Enhanced type annotations and refactored methods in multiple files to ensure compatibility and correct handling of result arrays within the codebase. These changes improve code reliability and maintainability.

* Add additional test cases and improve existing tests

Introduce new test cases for PdoStatement and PdoClass to cover non-array data scenarios and null object fetches. Enhance error handling and correct minor formatting inconsistencies for better code readability and maintenance.

* Format code and remove redundant test

Standardize parameter alignment in Pdo.php for better readability. Remove redundant exception test in PdoClassTest to streamline unit tests.

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: connor <[email protected]>
  • Loading branch information
dependabot[bot] and ActuallyConnor authored Nov 17, 2024
1 parent b3cf454 commit e118a25
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"require-dev": {
"phpunit/phpunit": "^9.6|^10.5|^11.3",
"phpunit/php-code-coverage": "^9.2|^10.1|^11.0",
"phpstan/phpstan": "^1.12",
"phpstan/phpstan": "^2.0",
"saggre/phpdocumentor-markdown": "^0.1.4"
},
"autoload": {
Expand Down
32 changes: 18 additions & 14 deletions src/Pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ class Pdo extends \PDO
private QueryLog $queryLog;

/**
* @param ResultCollection|null $collection
* @param ResultCollection|null $collection
*/
public function __construct(
ResultCollection $collection = null
) {
$this->mockedQueries = $collection ?? new ResultCollection();
$this->queryLog = new QueryLog();
$this->queryLog = new QueryLog();
}

/**
* @param string $query
* @param array<int|string,mixed> $options
* @param string $query
* @param array<int|string,mixed> $options
*
* @return PdoStatement
* @throws PseudoException
* @throws Throwable
Expand Down Expand Up @@ -82,9 +83,9 @@ public function exec(string $statement): false|int
}

/**
* @param string $query
* @param int|null $fetchMode
* @param mixed ...$fetchModeArgs
* @param string $query
* @param int|null $fetchMode
* @param mixed ...$fetchModeArgs
*
* @return PdoStatement
* @throws PseudoException|Throwable
Expand All @@ -105,7 +106,7 @@ public function query(string $query, ?int $fetchMode = null, mixed ...$fetchMode
}

/**
* @param string|null $name
* @param string|null $name
*
* @return string|false
* @throws PseudoException
Expand All @@ -119,7 +120,7 @@ public function lastInsertId(?string $name = null): string|false
return false;
}

return (string) $lastResult->getInsertId();
return (string)$lastResult->getInsertId();
}

/**
Expand All @@ -138,24 +139,27 @@ private function getLastResult(): Result|bool
}

/**
* @param string $filePath
* @param string $filePath
*/
public function save(string $filePath): void
{
file_put_contents($filePath, serialize($this->mockedQueries));
}

/**
* @param string $filePath
* @param string $filePath
*
* @throws PseudoException
*/
public function load(string $filePath): void
{
$fileContents = file_get_contents($filePath);

// @codeCoverageIgnoreStart
if ($fileContents === false) {
throw new PseudoException('Unable to read file: ' . $filePath);
}
// @codeCoverageIgnoreEnd

/** @var ResultCollection $resultCollection */
$resultCollection = unserialize($fileContents);
Expand All @@ -164,9 +168,9 @@ public function load(string $filePath): void
}

/**
* @param string $sql
* @param array<int|string,mixed>|null $params
* @param mixed $expectedResults
* @param string $sql
* @param array<int|string,mixed>|null $params
* @param mixed $expectedResults
*/
public function mock(string $sql, ?array $params = null, mixed $expectedResults = null): void
{
Expand Down
14 changes: 9 additions & 5 deletions src/PdoStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,16 @@ public function fetchColumn($column = 0): mixed
*/
public function fetchAll(int $mode = \PDO::FETCH_DEFAULT, mixed ...$args): array
{
$rows = $this->result->getRows() ?? [];
$rows = $this->result->getRows();

if (is_array($rows)) {
if (!empty($rows)) {
return array_map(
function ($row) use ($mode) {
return $this->processFetchedRow($row, $mode);
if (is_array($row)) {
return $this->processFetchedRow($row, $mode);
}

return $row;
},
$rows
);
Expand Down Expand Up @@ -260,10 +264,10 @@ public function errorInfo(): array
public function columnCount(): int
{
$rows = $this->result->getRows();
if (is_array($rows)) {
if (!empty($rows)) {
$row = array_shift($rows);

if (is_array($row)) {
if (is_array($row) && !empty($row)) {
return count(array_keys($row));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ public function setParams(array $params, bool $parameterize = false): void
/**
* @param array<int|string,mixed> $params
*
* @return mixed
* @return array<int|string,array<int|string,mixed>>|array<int|string,mixed>
* @throws PseudoException
*/
public function getRows(array $params = []): mixed
public function getRows(array $params = []): array
{
if (!empty($this->params) && empty($params)) {
$params = $this->params;
Expand Down
1 change: 1 addition & 0 deletions src/ResultCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function addQuery(string $sql, ?array $params = null, mixed $results = nu
$query = new ParsedQuery($sql);

if (is_array($results)) {
/** @var array<int|string, array<int|string, mixed>> $results */
$storedResults = new Result($results, $params);
} elseif ($results instanceof Result) {
$storedResults = $results;
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/PdoClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ public function testExec()
$this->assertEquals(5, $p->exec($sql));
}

/**
* This is technically an integration test and shouldn't be included here or in test coverage
* @return void
* @throws PseudoException
*/
public function testLoad()
{
$r = new ResultCollection();
Expand Down
28 changes: 26 additions & 2 deletions tests/Unit/PdoStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ public function testFetchUnrecognizedFetchMode(): void
$this->assertEquals([null], $fetchResult);
}

public function testFetchAllNonArrayData(): void
{
$r = new Result(null);
$s = new PdoStatement();
$s->setResult($r);
$fetchResult = $s->fetchAll();
$this->assertEmpty($fetchResult);
}

public function testFetchAllSubArrayNotArray(): void
{
$r = new Result([]);
$s = new PdoStatement();
$s->setResult($r);
$fetchResult = $s->fetchAll();
$this->assertEmpty($fetchResult);
}

public function testRowCount()
{
$s = new PdoStatement();
Expand Down Expand Up @@ -135,7 +153,7 @@ public function testColumnCount()
$this->assertEquals(2, $p->columnCount());
}

public function testColumnCountWhenZeo() : void
public function testColumnCountWhenZeo(): void
{
$statement = new PdoStatement();

Expand Down Expand Up @@ -242,6 +260,12 @@ public function testFailsToFetchObject(): void
$this->assertFalse($statement->fetchObject());
}

public function testNullFetchObject(): void
{
$statement = new PdoStatement();
$this->assertFalse($statement->fetchObject(null));
}

public function testExecute()
{
$row1 = [
Expand Down Expand Up @@ -293,7 +317,7 @@ public function testFetchColumn()
$this->assertEquals(false, $s->fetchColumn(0));
}

public function testGetIterator() : void
public function testGetIterator(): void
{
$rows = [
'id' => 1,
Expand Down

0 comments on commit e118a25

Please sign in to comment.