Skip to content

Commit

Permalink
Revue de code.
Browse files Browse the repository at this point in the history
  • Loading branch information
noelma committed Apr 29, 2021
1 parent 7a3f854 commit 90a1b75
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 185 deletions.
21 changes: 15 additions & 6 deletions src/Driver.php
Expand Up @@ -10,6 +10,7 @@

namespace Queryflatfile;

use Queryflatfile\Exception\Driver\DriverException;
use Queryflatfile\Exception\Driver\FileNotFoundException;
use Queryflatfile\Exception\Driver\FileNotReadableException;
use Queryflatfile\Exception\Driver\FileNotWritableException;
Expand Down Expand Up @@ -63,14 +64,17 @@ public function create(string $path, string $fileName, array $data = []): bool
if (!file_exists($path)) {
mkdir($path, 0775, true);
}
if (!file_exists($file)) {
$handle = fopen($file, 'w+');
fwrite($handle, $this->serializeData($data));
if (file_exists($file)) {
return false;
}

return fclose($handle);
$handle = fopen($file, 'w+');
if ($handle === false) {
throw new DriverException("$file file cannot be opened");
}
fwrite($handle, $this->serializeData($data));

return false;
return fclose($handle);
}

/**
Expand All @@ -86,7 +90,9 @@ public function read(string $path, string $fileName): array

$data = file_get_contents($file);

return $this->unserializeData($data);
return $data === false
? []
: $this->unserializeData($data);
}

/**
Expand All @@ -101,6 +107,9 @@ public function save(string $path, string $fileName, array $data): bool
$this->isWrite($file);

$handle = fopen($file, 'w');
if ($handle === false) {
throw new DriverException("$file file cannot be opened");
}
fwrite($handle, $this->serializeData($data));

return fclose($handle);
Expand Down
36 changes: 20 additions & 16 deletions src/Request.php
Expand Up @@ -10,6 +10,7 @@

namespace Queryflatfile;

use BadMethodCallException;
use Queryflatfile\Exception\Query\BadFunctionException;
use Queryflatfile\Exception\Query\ColumnsNotFoundException;
use Queryflatfile\Exception\Query\OperatorNotFound;
Expand Down Expand Up @@ -87,11 +88,11 @@ class Request extends RequestHandler
/**
* Réalise une requête sur un schéma de données
*
* @param Schema $sch
* @param Schema $schema
*/
public function __construct(Schema $sch)
public function __construct(Schema $schema)
{
$this->schema = $sch;
$this->schema = $schema;
}

/**
Expand All @@ -101,11 +102,9 @@ public function __construct(Schema $sch)
*/
public function __toString(): string
{
$output = '';
$output = 'SELECT * ';
if ($this->columns) {
$output .= 'SELECT ' . implode(', ', $this->columns) . ' ';
} else {
$output .= 'SELECT * ';
$output = 'SELECT ' . implode(', ', $this->columns) . ' ';
}
if ($this->from) {
$output .= "FROM $this->from ";
Expand Down Expand Up @@ -149,7 +148,7 @@ public function __toString(): string
* @param string $name Nom de la méthode appelée.
* @param array $args Pararètre de la méthode.
*
* @throws \BadMethodCallException
* @throws BadMethodCallException
*
* @return $this
*/
Expand All @@ -158,7 +157,7 @@ public function __call(string $name, array $args): self
$this->where = $this->where ?? new Where();

if (!method_exists($this->where, $name)) {
throw new \BadMethodCallException("The $name method not exist");
throw new BadMethodCallException("The $name method not exist");
}

$this->where->$name(...$args);
Expand All @@ -169,13 +168,13 @@ public function __call(string $name, array $args): self
/**
* Ajoute un schéma de données à notre requête.
*
* @param Schema $sch
* @param Schema $schema
*
* @return $this
*/
public function setSchema(Schema $sch): self
public function setSchema(Schema $schema): self
{
$this->schema = $sch;
$this->schema = $schema;

return $this;
}
Expand Down Expand Up @@ -390,7 +389,7 @@ protected static function arrayUniqueMultidimensional(array &$input): void
* @param string $table
* @param Where $where
*
* @return void;
* @return void
*/
protected function executeJoins(string $type, string $table, Where $where): void
{
Expand Down Expand Up @@ -450,7 +449,7 @@ protected function executeOrderBy(array &$data, array $orderBy): void
}
unset($order);

usort($data, static function ($a, $b) use ($orderBy) {
usort($data, static function ($a, $b) use ($orderBy): int {
$sorted = 0;

foreach ($orderBy as $field => $order) {
Expand All @@ -467,6 +466,7 @@ protected function executeOrderBy(array &$data, array $orderBy): void
}
}

/* @var int $sorted */
return $sorted;
});
}
Expand All @@ -489,11 +489,15 @@ protected function executeInsert(): void
/* Pour chaque ligne je vérifie si le nombre de colonne correspond au nombre valeur insérée. */
try {
/* Je prépare l'association clé=>valeur pour chaque ligne à insérer. */
$row = array_combine($this->columns, $values);
$row = array_combine($this->columns, $values);
} catch (\Exception $ex) {
throw new ColumnsNotFoundException('keys : ' . implode(',', $this->columns) . ' != ' . implode(',', $values));
}

if ($row === false) {
throw new ColumnsNotFoundException('keys : ' . implode(',', $this->columns) . ' != ' . implode(',', $values));
}

$data = [];
foreach ($schemaColumns as $field => $arg) {
/* Si mon champs existe dans le schema. */
Expand All @@ -508,7 +512,7 @@ protected function executeInsert(): void
}
/* Si mon champ n'existe pas et qu'il de type incrémental. */
if ($arg[ 'type' ] === TableBuilder::TYPE_INCREMENT) {
$increment++;
++$increment;
$data[ $field ] = $increment;

continue;
Expand Down
19 changes: 8 additions & 11 deletions src/RequestHandler.php
Expand Up @@ -194,7 +194,7 @@ public function select(...$columns)
*/
public function union(RequestInterface $request, string $type = self::UNION_SIMPLE)
{
$this->union[] = [ 'request' => $request, 'type' => $type ];
$this->union[] = compact('request', 'type');

return $this;
}
Expand Down Expand Up @@ -264,15 +264,12 @@ protected function init()
*/
private function join(string $type, string $table, $column, ?string $operator = null, ?string $value = null): void
{
if ($column instanceof \Closure) {
$where = new Where();
call_user_func_array($column, [ &$where ]);
} else {
$where = ( new Where() )
->where($column, $operator, $value);
}
$this->joins[] = [
'type' => $type, 'table' => $table, 'where' => $where
];
$where = new Where();

$column instanceof \Closure
? call_user_func_array($column, [ &$where ])
: $where->where($column, $operator, $value);

$this->joins[] = compact('type', 'table', 'where');
}
}
6 changes: 3 additions & 3 deletions src/Schema.php
Expand Up @@ -80,7 +80,7 @@ public function __construct(
string $name = 'schema',
DriverInterface $driver = null
) {
if (!\is_null($host)) {
if ($host !== null) {
$this->setConfig($host, $name, $driver);
}
}
Expand Down Expand Up @@ -522,7 +522,7 @@ protected static function add(
$schema[ 'fields' ][ $name ] = $params;

$increment = $params[ 'type' ] === TableBuilder::TYPE_INCREMENT
? 1
? 0
: null;

try {
Expand All @@ -534,7 +534,7 @@ protected static function add(
foreach ($dataTable as &$data) {
$data[ $name ] = $increment === null
? $valueDefault
: $increment++;
: ++$increment;
}

if ($params[ 'type' ] === TableBuilder::TYPE_INCREMENT) {
Expand Down
13 changes: 2 additions & 11 deletions src/TableBuilder.php
Expand Up @@ -373,16 +373,6 @@ public function build(): array
return $this->builder;
}

/**
* Retourne la liste des champs incrémentaux.
*
* @return int|null
*/
public function getIncrement(): ?int
{
return $this->increment;
}

/**
* Retour le schéma de la table.
*
Expand All @@ -409,7 +399,8 @@ public function getTableSchema(): array
*/
protected function checkPreviousBuild(string $opt): array
{
if (!($current = end($this->builder))) {
$current = end($this->builder);
if (!$current) {
throw new ColumnsNotFoundException("No column selected for $opt.");
}

Expand Down

0 comments on commit 90a1b75

Please sign in to comment.