Skip to content

Commit

Permalink
Add psalm and rename Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Mar 11, 2021
1 parent d687500 commit 310fe04
Show file tree
Hide file tree
Showing 23 changed files with 122 additions and 90 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ jobs:

- name: Run test suite
run: vendor/bin/phpunit tests

- name: Run static analysis
run: vendor/bin/psalm
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.idea
composer.lock
vendor
coverage
coverage
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Parable PHP ORM

## 0.11.0
- Added static analysis through psalm.
- Renamed `Exception` to `OrmException` for clarity.

## 0.10.0

_Changes_
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ dependencies:
--no-plugins \
--no-scripts

psalm:
vendor/bin/psalm --clear-cache
vendor/bin/psalm

tests: dependencies
vendor/bin/phpunit --verbose tests

coverage: dependencies
rm -rf ./coverage
vendor/bin/phpunit --coverage-html ./coverage tests
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage tests

tests-clean:
vendor/bin/phpunit --verbose tests

coverage-clean:
rm -rf ./coverage
vendor/bin/phpunit --coverage-html ./coverage tests
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage tests
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"parable-php/query": "^0.4.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
"phpunit/phpunit": "^8.0",
"vimeo/psalm": "^4.6"
},
"autoload": {
"psr-4": {
Expand Down
19 changes: 19 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<UnresolvableInclude errorLevel="suppress" />
</issueHandlers>
</psalm>
8 changes: 4 additions & 4 deletions src/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function fromDatabaseItem(Container $container, string $primaryKey
$entity->validatePrimaryKeyExistsOnEntity($primaryKey);

if (!isset($values[$primaryKey])) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Could not set primary key '%s' on entity %s from database values",
$primaryKey,
static::class
Expand All @@ -47,7 +47,7 @@ public static function fromDatabaseItem(Container $container, string $primaryKey

foreach ($values as $property => $value) {
if (!property_exists($entity, $property)) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Property '%s' does not exist on entity %s",
$property,
static::class
Expand Down Expand Up @@ -148,7 +148,7 @@ protected function getSetterForProperty(string $property): string
}

if (!method_exists($this, $setter)) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Setter method '%s' not defined on entity %s",
$setter,
static::class
Expand All @@ -161,7 +161,7 @@ protected function getSetterForProperty(string $property): string
protected function validatePrimaryKeyExistsOnEntity(string $key): void
{
if (!property_exists($this, $key)) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Primary key property '%s' does not exist on entity %s",
$key,
static::class
Expand Down
12 changes: 6 additions & 6 deletions src/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public function createEntityClass(): AbstractEntity
$entityClass = $this->getEntityClass();

if (!class_exists($entityClass)) {
throw new Exception(sprintf("Traits class '%s' does not exist.", $entityClass));
throw new OrmException(sprintf("Traits class '%s' does not exist.", $entityClass));
}

$entity = $this->container->build($this->getEntityClass());

if (!($entity instanceof AbstractEntity)) {
throw new Exception(sprintf("Class '%s' does not extend AbstractEntity.", $entityClass));
throw new OrmException(sprintf("Class '%s' does not extend AbstractEntity.", $entityClass));
}

return $entity;
Expand Down Expand Up @@ -106,7 +106,7 @@ public function findUniqueBy(callable $callable): ?AbstractEntity
$entities = $this->findBy($callable);

if (count($entities) > 1) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Found more than one of '%s'",
$this->getEntityClass()
));
Expand Down Expand Up @@ -269,7 +269,7 @@ public function clearDeferredDeletes(): void
protected function getBuilder(): Builder
{
if (!$this->database->isConnected()) {
throw new Exception('Cannot use repository methods without database connection.');
throw new OrmException('Cannot use repository methods without database connection.');
}

if ($this->builder === null) {
Expand Down Expand Up @@ -328,7 +328,7 @@ protected function getPrimaryKeysFromEntities(array $entities): array
$this->validateEntityCorrectClass($entity);

if (!$this->isStored($entity)) {
throw new Exception('Cannot delete entity that is not stored.');
throw new OrmException('Cannot delete entity that is not stored.');
}

$primaryKeys[] = $entity->getPrimaryKey($this->getPrimaryKey());
Expand All @@ -351,7 +351,7 @@ protected function validateEntityCorrectClass(AbstractEntity $entity): void
return;
}

throw new Exception(sprintf(
throw new OrmException(sprintf(
"Expected '%s', got '%s' instead. Cannot handle these classes.",
$entityClass,
get_class($entity)
Expand Down
14 changes: 7 additions & 7 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Database
public function setConnectionClass(string $connectionClass): void
{
if (!is_subclass_of($connectionClass, PDO::class)) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Class %s does not extend PDO, which is required",
$connectionClass
));
Expand Down Expand Up @@ -119,7 +119,7 @@ public function setErrorMode(int $errorMode): void
[PDO::ERRMODE_SILENT, PDO::ERRMODE_WARNING, PDO::ERRMODE_EXCEPTION],
true
)) {
throw new Exception(sprintf("Invalid error mode set: '%d'", $errorMode));
throw new OrmException(sprintf("Invalid error mode set: '%d'", $errorMode));
}

$this->errorMode = $errorMode;
Expand Down Expand Up @@ -181,13 +181,13 @@ protected function createConnectionByType(int $type): ?PDO
return $this->createSqliteConnection();
}

throw new Exception(sprintf("Cannot create connection for invalid database type: '%d'", $type));
throw new OrmException(sprintf("Cannot create connection for invalid database type: '%d'", $type));
}

protected function createMySQLConnection(): PDO
{
if ($this->host === null) {
throw new Exception('MySQL requires a host.');
throw new OrmException('MySQL requires a host.');
}

$connection = $this->createConnection(...$this->buildMySQLConnectionValues());
Expand Down Expand Up @@ -216,11 +216,11 @@ protected function buildMySQLConnectionValues(): array
protected function createSqliteConnection(): PDO
{
if ($this->databaseName === null) {
throw new Exception('Sqlite requires a database.');
throw new OrmException('Sqlite requires a database.');
}

if ($this->databaseName !== ':memory:' && !is_readable($this->databaseName)) {
throw new Exception(sprintf("Could not read Sqlite database: %s", $this->databaseName));
throw new OrmException(sprintf("Could not read Sqlite database: %s", $this->databaseName));
}

$dsn = sprintf('sqlite:%s', $this->databaseName);
Expand All @@ -241,7 +241,7 @@ public function query(string $query): array
$pdoStatement = $this->connection->query($query, PDO::FETCH_ASSOC);

if (!$pdoStatement) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Could not perform query: '%s', reason: %s: '%s'",
$query,
$this->connection->errorCode() ?? 'unknown',
Expand Down
2 changes: 1 addition & 1 deletion src/Exception.php → src/OrmException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Parable\Orm;

class Exception extends \Exception
class OrmException extends \Exception
{
}
6 changes: 3 additions & 3 deletions src/PropertyTypes/BooleanPropertyTyper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Parable\Orm\PropertyTypes;

use Parable\Orm\Exception;
use Parable\Orm\OrmException;

class BooleanPropertyTyper implements PropertyTyper
{
public function type($value): bool
{
if ($value !== '1' && $value !== '0') {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Could not type '%s' as boolean",
$value
));
Expand All @@ -21,7 +21,7 @@ public function type($value): bool
public function untype($value): string
{
if (!is_bool($value)) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Could not untype '%s' from boolean",
$value
));
Expand Down
6 changes: 3 additions & 3 deletions src/PropertyTypes/DatePropertyTyper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DateTimeImmutable;
use DateTimeInterface;
use Parable\Orm\Database;
use Parable\Orm\Exception;
use Parable\Orm\OrmException;

class DatePropertyTyper implements PropertyTyper
{
Expand All @@ -14,7 +14,7 @@ public function type($value): DateTimeImmutable
$date = DateTimeImmutable::createFromFormat(Database::DATE_SQL, $value);

if ($date === false) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Could not type '%s' as date with format %s",
$value,
Database::DATE_SQL
Expand All @@ -27,7 +27,7 @@ public function type($value): DateTimeImmutable
public function untype($value): string
{
if (!($value instanceof DateTimeInterface)) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Could not untype '%s' as date from DateTimeInterface with format %s",
$value,
Database::DATE_SQL
Expand Down
6 changes: 3 additions & 3 deletions src/PropertyTypes/DateTimePropertyTyper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DateTimeImmutable;
use DateTimeInterface;
use Parable\Orm\Database;
use Parable\Orm\Exception;
use Parable\Orm\OrmException;

class DateTimePropertyTyper implements PropertyTyper
{
Expand All @@ -14,7 +14,7 @@ public function type($value): DateTimeImmutable
$datetime = DateTimeImmutable::createFromFormat(Database::DATETIME_SQL, $value);

if ($datetime === false) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Could not type '%s' as datetime with format %s",
$value,
Database::DATETIME_SQL
Expand All @@ -27,7 +27,7 @@ public function type($value): DateTimeImmutable
public function untype($value): string
{
if (!($value instanceof DateTimeInterface)) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Could not untype '%s' as datetime from DateTimeInterface with format %s",
$value,
Database::DATETIME_SQL
Expand Down
6 changes: 3 additions & 3 deletions src/PropertyTypes/IntegerPropertyTyper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Parable\Orm\PropertyTypes;

use Parable\Orm\Exception;
use Parable\Orm\OrmException;

class IntegerPropertyTyper implements PropertyTyper
{
public function type($value): int
{
if (!is_numeric($value)) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Could not type '%s' as integer",
$value
));
Expand All @@ -21,7 +21,7 @@ public function type($value): int
public function untype($value): string
{
if (!is_int($value)) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Could not untype '%s' from integer",
$value
));
Expand Down
6 changes: 3 additions & 3 deletions src/PropertyTypes/TimePropertyTyper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DateTimeImmutable;
use DateTimeInterface;
use Parable\Orm\Database;
use Parable\Orm\Exception;
use Parable\Orm\OrmException;

class TimePropertyTyper implements PropertyTyper
{
Expand All @@ -14,7 +14,7 @@ public function type($value): DateTimeImmutable
$time = DateTimeImmutable::createFromFormat(Database::TIME_SQL, $value);

if ($time === false) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Could not type '%s' as time with format %s",
$value,
Database::TIME_SQL
Expand All @@ -27,7 +27,7 @@ public function type($value): DateTimeImmutable
public function untype($value): string
{
if (!($value instanceof DateTimeInterface)) {
throw new Exception(sprintf(
throw new OrmException(sprintf(
"Could not untype '%s' as time from DateTimeInterface with format %s",
$value,
Database::TIME_SQL
Expand Down
Loading

0 comments on commit 310fe04

Please sign in to comment.