Skip to content

Commit

Permalink
Bring PHPStan to level 9 and adding workflow (#4)
Browse files Browse the repository at this point in the history
* Level 1

* Level 2

* Level 3

* Level 4

* Level 4

* Level 6

* Level 7

* Level 7=8

* Adding workflow

* WIP

* Level 9
  • Loading branch information
ActuallyConnor authored Oct 12, 2024
1 parent e0d1ac4 commit e28e5bb
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 103 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,29 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: phpunit

- name: Run tests - phpunit
run: |
composer install
./vendor/bin/phpunit --configuration phpunit.xml.dist tests/Unit
static-analysis:
name: Static Analysis
runs-on: ubuntu-22.04
strategy:
matrix:
php-versions: [ '8.0', '8.1', '8.2', '8.3' ]

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}

- name: Run tests - phpunit
run: |
composer install
./vendor/bin/phpstan analyse --configuration phpstan.neon src
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"require-dev": {
"phpunit/phpunit": "^9.6|^10.5|^11.3",
"phpunit/php-code-coverage": "^9.2|^10.1|^11.0"
"phpunit/php-code-coverage": "^9.2|^10.1|^11.0",
"phpstan/phpstan": "^1.12"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 9
paths:
- src
phpVersion: 80200
3 changes: 2 additions & 1 deletion src/ParsedQuery.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Pseudo;

class ParsedQuery
Expand All @@ -15,7 +16,7 @@ public function __construct(string $query)
$this->hash = sha1($query);
}

public function isEqualTo($query)
public function isEqualTo(ParsedQuery|string $query): bool
{
if (!($query instanceof self)) {
$query = new self($query);
Expand Down
73 changes: 44 additions & 29 deletions src/Pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@ 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();
}

/**
* @throws PseudoException|Throwable
* @param string $query
* @param array<int|string,mixed> $options
* @return PdoStatement
* @throws PseudoException
* @throws Throwable
*/
public function prepare($query, $options = null) : PdoStatement
public function prepare(string $query, array $options = []): PdoStatement
{
$result = $this->mockedQueries->getResult($query);

return new PdoStatement($result, $this->queryLog, $query);
}

public function beginTransaction() : bool
public function beginTransaction(): bool
{
if (!$this->inTransaction) {
$this->inTransaction = true;
Expand All @@ -43,7 +47,7 @@ public function beginTransaction() : bool
return false;
}

public function commit() : bool
public function commit(): bool
{
if ($this->inTransaction()) {
$this->inTransaction = false;
Expand All @@ -54,7 +58,7 @@ public function commit() : bool
return false;
}

public function rollBack() : bool
public function rollBack(): bool
{
if ($this->inTransaction()) {
$this->inTransaction = false;
Expand All @@ -65,27 +69,27 @@ public function rollBack() : bool
return false;
}

public function inTransaction() : bool
public function inTransaction(): bool
{
return $this->inTransaction;
}

public function exec($statement) : false|int
public function exec($statement): false|int
{
$result = $this->query($statement);

return $result->rowCount();
}

/**
* @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
*/
public function query(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs) : PdoStatement
public function query(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs): PdoStatement
{
if ($this->mockedQueries->exists($query)) {
$result = $this->mockedQueries->getResult($query);
Expand All @@ -101,27 +105,28 @@ public function query(string $query, ?int $fetchMode = null, mixed ...$fetchMode
}

/**
* @param null $name
* @param string|null $name
*
* @return false|string
* @return string|false
* @throws PseudoException
* @throws Throwable
*/
public function lastInsertId($name = null) : false|string
public function lastInsertId(?string $name = null): string|false
{
$result = $this->getLastResult();
$lastResult = $this->getLastResult();

if (!$result) {
if (is_bool($lastResult)) {
return false;
}

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

/**
* @return Result|false
* @return Result|bool
* @throws PseudoException|Throwable
*/
private function getLastResult() : Result|false
private function getLastResult(): Result|bool
{
try {
$lastQuery = $this->queryLog[count($this->queryLog) - 1];
Expand All @@ -133,35 +138,45 @@ private function getLastResult() : Result|false
}

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

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

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

/** @var ResultCollection $resultCollection */
$resultCollection = unserialize($fileContents);

$this->mockedQueries = $resultCollection;
}

/**
* @param string $sql
* @param array|null $params
* @param array<int|string,mixed>|null $params
* @param mixed $expectedResults
*/
public function mock(string $sql, ?array $params = null, mixed $expectedResults = null) : void
public function mock(string $sql, ?array $params = null, mixed $expectedResults = null): void
{
$this->mockedQueries->addQuery($sql, $params, $expectedResults);
}

/**
* @return ResultCollection
*/
public function getMockedQueries() : ResultCollection
public function getMockedQueries(): ResultCollection
{
return $this->mockedQueries;
}
Expand Down
Loading

0 comments on commit e28e5bb

Please sign in to comment.