Skip to content

Commit

Permalink
fix: format files
Browse files Browse the repository at this point in the history
  • Loading branch information
rancoud committed Apr 27, 2023
1 parent 6924f9f commit 0f062ed
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 112 deletions.
47 changes: 24 additions & 23 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use PhpCsFixer\Finder;

$rules = [
'@Symfony' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => [
'array_syntax' => [
'syntax' => 'short'
],
'binary_operator_spaces' => [
Expand All @@ -19,32 +19,32 @@
'concat_space' => [
'spacing' => 'one'
],
'declare_strict_types' => true,
'global_namespace_import' => false,
'declare_strict_types' => true,
'global_namespace_import' => false,
'linebreak_after_opening_tag' => true,
'mb_str_functions' => true,
'native_function_invocation' => [
'mb_str_functions' => true,
'native_function_invocation' => [
'include' => [
'@all'
]
],
'no_php4_constructor' => true,
'no_superfluous_phpdoc_tags' => false,
'no_php4_constructor' => true,
'no_superfluous_phpdoc_tags' => false,
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'php_unit_strict' => true,
'phpdoc_order' => true,
'semicolon_after_instruction' => true,
'single_import_per_statement' => false,
'strict_comparison' => true,
'strict_param' => true,
'single_line_throw' => false,
'trailing_comma_in_multiline' => false,
'yoda_style' => [
'equal' => false,
'identical' => false,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'php_unit_strict' => true,
'phpdoc_order' => true,
'semicolon_after_instruction' => true,
'single_import_per_statement' => false,
'strict_comparison' => true,
'strict_param' => true,
'single_line_throw' => false,
'trailing_comma_in_multiline' => false,
'yoda_style' => [
'equal' => false,
'identical' => false,
'less_and_greater' => false
],
];
Expand All @@ -59,7 +59,8 @@
->ignoreVCS(true);

$config = new Config();

return $config->setFinder($finder)
->setRules($rules)
->setRiskyAllowed(true)
->setUsingCache(true);
->setUsingCache(true);
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,15 @@ You can use JsonOutput trait for adding json format for the model.
## Field Constructor
### Settings
#### Mandatory
| Parameter | Type | Description |
| --- | --- | --- |
| type | string | type of field, values used : int \| float \| char \| varchar \| text \| date \| datetime \| time \| timestamp \| year |
| Parameter | Type | Description |
|-----------|--------|-----------------------------------------------------------------------------------------------------------------------|
| type | string | type of field, values used : int \| float \| char \| varchar \| text \| date \| datetime \| time \| timestamp \| year |

#### Optionnals
| Parameter | Type | Default value | Description |
| --- | --- | --- | --- |
| rules | array | [] | rules for checking values, values used : pk \| fk \| unsigned \| email \| not_null \| max \| min \| range \| Rancoud\Model\CustomRule |
| default | mixed | false | default value when none given |
| Parameter | Type | Default value | Description |
|-----------|-------|---------------|---------------------------------------------------------------------------------------------------------------------------------------|
| rules | array | [] | rules for checking values, values used : pk \| fk \| unsigned \| email \| not_null \| max \| min \| range \| Rancoud\Model\CustomRule |
| default | mixed | false | default value when none given |

## Field Methods
* isPrimaryKey(): bool
Expand All @@ -198,8 +198,8 @@ You can use JsonOutput trait for adding json format for the model.
## Model Constructor
### Settings
#### Mandatory
| Parameter | Type | Description |
| --- | --- | --- |
| Parameter | Type | Description |
|-----------|----------------------------|-------------------|
| $database | \Rancoud\Database\Database | Database Instance |

## Model Methods
Expand Down
16 changes: 8 additions & 8 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class Field

protected array $range = [null, null];

protected string $patternTime = '[0-2]{1}[0-9]{1}\:[0-5]{1}[0-9]{1}\:[0-5]{1}[0-9]{1}';
protected string $patternDate = '[1-9]{1}[0-9]{3}\-[0-1]{1}[0-9]{1}\-[0-3]{1}[0-9]{1}';
protected string $patternTime = '[0-2][0-9]:[0-5][0-9]:[0-5][0-9]';
protected string $patternDate = '[1-9][0-9]{3}-[0-1][0-9]-[0-3][0-9]';

/**
* Field constructor.
*
* @param string $type
* @param array $rules
* @param bool $default
* @param mixed $default
*
* @throws FieldException
*/
Expand Down Expand Up @@ -234,7 +234,7 @@ public function getDefault()
public function formatValue($value)
{
if ($value === false) {
return $this->applyDefault($value);
return $this->applyDefault(false);
}

if ($this->notNull === false && $value === null) {
Expand Down Expand Up @@ -410,7 +410,7 @@ protected function convertToTime($value): string
throw new FieldException('Invalid time value');
}

$hour = (int) (\mb_substr($time, 0, 2));
$hour = (int) \mb_substr($time, 0, 2);
if ($hour > 23) {
throw new FieldException('Invalid time value');
}
Expand All @@ -423,7 +423,7 @@ protected function convertToTime($value): string
*
* @throws FieldException
*
* @return false|int|string
* @return false|string
*/
protected function convertToTimestamp($value)
{
Expand Down Expand Up @@ -622,9 +622,9 @@ protected function applyRules($value)
*
* @throws FieldException
*
* @return mixed
* @return string
*/
protected function applyRuleEmail(string $value)
protected function applyRuleEmail(string $value): string
{
$pos = \mb_strpos($value, '@');
$length = \mb_strlen($value);
Expand Down
4 changes: 2 additions & 2 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function isRowsCount(array $args): bool
return false;
}

return (int) ($args[self::$rowsCount]) === 1;
return (int) $args[self::$rowsCount] === 1;
}

/**
Expand All @@ -45,7 +45,7 @@ public static function hasLimit(array $args): bool
return true;
}

return (int) ($args[self::$noLimit]) !== 1;
return (int) $args[self::$noLimit] !== 1;
}

/**
Expand Down
32 changes: 19 additions & 13 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ protected function allRows(array $params, array $validFields = []): array
* @param array $params
*
* @return string
*
* @noinspection PhpUnusedParameterInspection
*/
protected function getSqlAllSelectAndFillSqlParams(array $params): string
{
Expand All @@ -299,6 +301,8 @@ protected function getSqlAllSelectAndFillSqlParams(array $params): string
* @param array $params
*
* @return string
*
* @noinspection PhpUnusedParameterInspection
*/
protected function getSqlAllJoinAndFillSqlParams(array $params): string
{
Expand Down Expand Up @@ -386,7 +390,6 @@ public function create(array $args): int
$sql = $this->checkNotNullFieldIsPresent($sql);

try {
/* @noinspection PhpFieldAssignmentTypeMismatchInspection */
$this->lastInsertId = $this->database->insert($sql, $this->sqlParams, true);
} catch (DatabaseException $de) {
$this->addErrorMessage('Error creating');
Expand Down Expand Up @@ -513,6 +516,7 @@ protected function removeParameters(): void
* @param string $mode
*
* @throws ModelException
*
* @noinspection PhpDocRedundantThrowsInspection
*/
protected function treatParametersAfterClean(string $mode): void
Expand All @@ -525,6 +529,7 @@ protected function treatParametersAfterClean(string $mode): void
* @param $params
*
* @throws ModelException
*
* @noinspection PhpDocRedundantThrowsInspection
*/
protected function beforeCallbacks($callbacks, &$sql, &$params): void
Expand All @@ -549,6 +554,7 @@ protected function beforeCallbacks($callbacks, &$sql, &$params): void
* @param mixed ...$params
*
* @throws ModelException
*
* @noinspection PhpDocRedundantThrowsInspection
*/
protected function afterCallbacks($callbacks, ...$params): void
Expand Down Expand Up @@ -636,53 +642,53 @@ protected function afterDelete($params): void
}

/**
* @param string $name
* @param \Closure $callback
* @param string $name
* @param \Closure|array $callback
*/
public function addBeforeCreate(string $name, $callback): void
{
$this->callbacksCud['bc'][$name] = $callback;
}

/**
* @param string $name
* @param \Closure $callback
* @param string $name
* @param \Closure|array $callback
*/
public function addAfterCreate(string $name, $callback): void
{
$this->callbacksCud['ac'][$name] = $callback;
}

/**
* @param string $name
* @param \Closure $callback
* @param string $name
* @param \Closure|array $callback
*/
public function addBeforeUpdate(string $name, $callback): void
{
$this->callbacksCud['bu'][$name] = $callback;
}

/**
* @param string $name
* @param \Closure $callback
* @param string $name
* @param \Closure|array $callback
*/
public function addAfterUpdate(string $name, $callback): void
{
$this->callbacksCud['au'][$name] = $callback;
}

/**
* @param string $name
* @param \Closure $callback
* @param string $name
* @param \Closure|array $callback
*/
public function addBeforeDelete(string $name, $callback): void
{
$this->callbacksCud['bd'][$name] = $callback;
}

/**
* @param string $name
* @param \Closure $callback
* @param string $name
* @param \Closure|array $callback
*/
public function addAfterDelete(string $name, $callback): void
{
Expand Down
56 changes: 24 additions & 32 deletions tests/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,44 +39,36 @@ public function testFieldExceptionInvalidRule(): void
*/
public function testFieldKeyType(): void
{
try {
$field = new Field('int', ['unsigned', 'not_null', 'pk']);
static::assertTrue($field->isPrimaryKey());
static::assertFalse($field->isForeignKey());
$field = new Field('int', ['unsigned', 'not_null', 'pk']);
static::assertTrue($field->isPrimaryKey());
static::assertFalse($field->isForeignKey());

$field = new Field('int', ['unsigned', 'not_null', 'fk']);
static::assertFalse($field->isPrimaryKey());
static::assertTrue($field->isForeignKey());
} catch (FieldException $e) {
throw $e;
}
$field = new Field('int', ['unsigned', 'not_null', 'fk']);
static::assertFalse($field->isPrimaryKey());
static::assertTrue($field->isForeignKey());
}

/**
* @throws FieldException
*/
public function testFieldDefault(): void
{
try {
$field = new Field('int', ['unsigned', 'not_null', 'pk']);
static::assertFalse($field->getDefault());

$field = new Field('int', ['unsigned', 'not_null', 'pk'], 8);
static::assertSame(8, $field->getDefault());
$field = new Field('int', ['unsigned', 'not_null', 'pk']);
static::assertFalse($field->getDefault());

$countAssert = 1;
$field = new Field('int', ['unsigned', 'not_null', 'pk'], 8);
static::assertSame(8, $field->getDefault());

try {
new Field('varchar', ['email'], '8');
} catch (FieldException $fieldException) {
static::assertSame('Invalid email value', $fieldException->getMessage());
--$countAssert;
}
$countAssert = 1;

static::assertSame(0, $countAssert);
} catch (FieldException $e) {
throw $e;
try {
new Field('varchar', ['email'], '8');
} catch (FieldException $fieldException) {
static::assertSame('Invalid email value', $fieldException->getMessage());
--$countAssert;
}

static::assertSame(0, $countAssert);
}

// region Data Provider
Expand Down Expand Up @@ -1459,12 +1451,12 @@ public function data(): array
/**
* @dataProvider data
*
* @param string fieldType
* @param array rules
* @param mixed default
* @param array input
* @param array expected
* @param array message
* @param string $fieldType
* @param array $rules
* @param mixed $default
* @param array $input
* @param array $expected
* @param array $message
*
* @throws FieldException
*/
Expand Down
2 changes: 0 additions & 2 deletions tests/ImplementModel.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

/** @noinspection PhpUndefinedClassInspection */

declare(strict_types=1);

namespace tests;
Expand Down
2 changes: 0 additions & 2 deletions tests/ImplementationJsonOutput.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

/** @noinspection PhpUndefinedClassInspection */

declare(strict_types=1);

namespace tests;
Expand Down
Loading

0 comments on commit 0f062ed

Please sign in to comment.