Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add DataConverter to convert types #8230

Merged
merged 38 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
82085f2
feat: add Database\DataConverter class
kenjis Nov 18, 2023
69dd4cc
chore: adds skip_violations
kenjis Nov 18, 2023
caf51d4
refactor: use get_debug_type() instead of var_export()
kenjis Nov 20, 2023
8554b71
test: extract createDataConverter()
kenjis Nov 22, 2023
c15bbbe
refactor: fix PHPStan types
kenjis Nov 22, 2023
caa43a0
chore: vendor/bin/psalm --set-baseline=psalm-baseline.xml
kenjis Nov 22, 2023
cf3bb3f
docs: fix doc comment DB column type
kenjis Nov 22, 2023
e040667
docs: fix PHPDoc types
kenjis Nov 23, 2023
479d3fb
feat: improve error messages
kenjis Nov 23, 2023
83574ab
refactor: use var_export()
kenjis Nov 23, 2023
323a3d3
refactor: use static::class
kenjis Nov 23, 2023
3447d01
docs: add {@inheritDoc}
kenjis Nov 23, 2023
943599b
docs: fix TDbColumn type
kenjis Nov 23, 2023
8630f5d
fix: remove uneeded if conditions
kenjis Nov 23, 2023
75b16ae
docs: improve doc comments for conversions
kenjis Nov 23, 2023
c2ddfe9
docs: remove {@inheritDoc}
kenjis Nov 23, 2023
905a1a5
refactor: rename method names
kenjis Nov 23, 2023
938ae6e
refactor: change namespace to CodeIgniter\DataConverter
kenjis Nov 24, 2023
e42704d
feat: datetime supports format
kenjis Nov 24, 2023
7e4049e
refactor: rename variable names
kenjis Nov 24, 2023
4cafb96
feat: add nullable check
kenjis Nov 24, 2023
896cca5
chore: vendor/bin/psalm --set-baseline=psalm-baseline.xml
kenjis Nov 26, 2023
c7b6760
refactor: add `declare(strict_types=1)`
kenjis Dec 7, 2023
aee23f3
docs: improve comments
kenjis Dec 28, 2023
38a88c9
docs: update comments in Entity/Cast/CastInterface
kenjis Jan 23, 2024
f2417d3
refactor: make method names to be same as Entity CastInterface
kenjis Jan 23, 2024
8200f8e
refactor: move DataConverter/Cast to DataCaster/Cast
kenjis Jan 23, 2024
e98c902
feat: add DataCaster class
kenjis Jan 23, 2024
ac9c536
refactor: use DataCaster in DataConverter
kenjis Jan 23, 2024
31d8d79
refactor: use DataCaster in Entity
kenjis Jan 23, 2024
dd44603
chore: update phpstan-baseline
kenjis Jan 23, 2024
868402d
chore: update deptrac.yaml
kenjis Jan 23, 2024
07e8b63
chore: update psalm-baseline
kenjis Jan 23, 2024
590f580
test: add missing parent::__construct()
kenjis Jan 23, 2024
942eddb
docs: remove PHPDoc @template
kenjis Jan 23, 2024
567372e
docs: make @param more strict
kenjis Jan 23, 2024
029d118
docs: fix doc comments in Entity/Cast/CastInterface
kenjis Jan 29, 2024
7199ac4
docs: update @param style
kenjis Jan 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ parameters:
collectors:
- type: className
regex: ^Codeigniter\\Database\\.*
- name: DataCaster
collectors:
- type: className
regex: ^Codeigniter\\DataCaster\\.*
- name: DataConverter
collectors:
- type: className
regex: ^Codeigniter\\DataConverter\\.*
- name: Email
collectors:
- type: className
Expand Down Expand Up @@ -166,10 +174,16 @@ parameters:
- Entity
- Events
- I18n
DataCaster:
- I18n
- URI
DataConverter:
- DataCaster
Email:
- I18n
- Events
Entity:
- DataCaster
- I18n
Files:
- I18n
Expand Down Expand Up @@ -228,6 +242,11 @@ parameters:
- CodeIgniter\HTTP\Header
- CodeIgniter\HTTP\IncomingRequest
- CodeIgniter\HTTP\ResponseInterface
CodeIgniter\DataCaster\DataCaster:
- CodeIgniter\Entity\Cast\CastInterface
- CodeIgniter\Entity\Exceptions\CastException
CodeIgniter\DataCaster\Exceptions\CastException:
- CodeIgniter\Entity\Exceptions\CastException
CodeIgniter\Entity\Cast\URICast:
- CodeIgniter\HTTP\URI
CodeIgniter\Log\Handlers\ChromeLoggerHandler:
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 3,
'count' => 1,
'path' => __DIR__ . '/system/Entity/Entity.php',
];
$ignoreErrors[] = [
Expand Down
40 changes: 1 addition & 39 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<files psalm-version="5.20.0@3f284e96c9d9be6fe6b15c79416e1d1903dcfef4">
<file src="app/Config/View.php">
<UndefinedDocblockClass>
<code><![CDATA[array<string, ParserCallableString>]]></code>
Expand All @@ -26,12 +26,6 @@
<code>Memcache|Memcached</code>
</UndefinedDocblockClass>
</file>
<file src="system/Commands/Utilities/Routes/ControllerMethodReader.php">
<DuplicateArrayKey>
<code>$routeWithoutController</code>
<code>$routeWithoutController</code>
</DuplicateArrayKey>
</file>
<file src="system/Config/View.php">
<UndefinedDocblockClass>
<code><![CDATA[array<string, array<parser_callable_string>|parser_callable_string|parser_callable>]]></code>
Expand Down Expand Up @@ -91,15 +85,6 @@
<code><![CDATA[$this->db->transStatus]]></code>
</InaccessibleProperty>
</file>
<file src="system/Database/OCI8/Connection.php">
<UndefinedConstant>
<code>OCI_COMMIT_ON_SUCCESS</code>
<code>OCI_COMMIT_ON_SUCCESS</code>
<code>OCI_COMMIT_ON_SUCCESS</code>
<code>OCI_NO_AUTO_COMMIT</code>
<code>SQLT_CHR</code>
</UndefinedConstant>
</file>
<file src="system/Debug/Toolbar/Views/toolbar.tpl.php">
<InaccessibleMethod>
<code>renderTimeline</code>
Expand Down Expand Up @@ -132,11 +117,6 @@
<code>#[ReturnTypeWillChange]</code>
</MissingImmutableAnnotation>
</file>
<file src="system/Test/ControllerResponse.php">
<UnsupportedPropertyReferenceUsage>
<code><![CDATA[$this->dom = &$this->domParser]]></code>
</UnsupportedPropertyReferenceUsage>
</file>
<file src="system/View/Parser.php">
<UndefinedDocblockClass>
<code><![CDATA[array<string, array<parser_callable_string>|parser_callable_string|parser_callable>]]></code>
Expand Down Expand Up @@ -164,11 +144,6 @@
<code>$this</code>
</InvalidScope>
</file>
<file src="tests/system/CLI/ConsoleTest.php">
<DuplicateArrayKey>
<code>$command</code>
</DuplicateArrayKey>
</file>
<file src="tests/system/CommonFunctionsTest.php">
<UndefinedClass>
<code>UnexsistenceClass</code>
Expand All @@ -181,19 +156,6 @@
<code><![CDATA['Config\TestRegistrar']]></code>
</UndefinedClass>
</file>
<file src="tests/system/Database/BaseConnectionTest.php">
<InaccessibleProperty>
<code><![CDATA[$db->username]]></code>
<code><![CDATA[$db->username]]></code>
</InaccessibleProperty>
</file>
<file src="tests/system/Database/Live/OCI8/CallStoredProcedureTest.php">
<UndefinedConstant>
<code>OCI_ASSOC</code>
<code>OCI_B_CURSOR</code>
<code>OCI_RETURN_NULLS</code>
</UndefinedConstant>
</file>
<file src="tests/system/Entity/EntityTest.php">
<EmptyArrayAccess>
<code>$current[$key]</code>
Expand Down
41 changes: 41 additions & 0 deletions system/DataCaster/Cast/ArrayCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\DataCaster\Cast;

/**
* Class ArrayCast
*
* (PHP) [array --> string] --> (DB driver) --> (DB column) string
* [ <-- string] <-- (DB driver) <-- (DB column) string
*/
class ArrayCast extends BaseCast implements CastInterface
{
public static function get(mixed $value, array $params = []): array
{
if (! is_string($value)) {
self::invalidTypeValueError($value);
}

if ((strpos($value, 'a:') === 0 || strpos($value, 's:') === 0)) {
$value = unserialize($value, ['allowed_classes' => false]);
}

return (array) $value;
}

public static function set(mixed $value, array $params = []): string
{
return serialize($value);
}
}
39 changes: 39 additions & 0 deletions system/DataCaster/Cast/BaseCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\DataCaster\Cast;

use TypeError;

abstract class BaseCast implements CastInterface
{
public static function get(mixed $value, array $params = []): mixed
{
return $value;
}

public static function set(mixed $value, array $params = []): mixed
{
return $value;
}

protected static function invalidTypeValueError(mixed $value): never
{
$message = '[' . static::class . '] Invalid value type: ' . get_debug_type($value);
if (is_scalar($value)) {
$message .= ', and its value: ' . var_export($value, true);
}

throw new TypeError($message);
}
}
36 changes: 36 additions & 0 deletions system/DataCaster/Cast/BooleanCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\DataCaster\Cast;

/**
* Class BooleanCast
*
* (PHP) [bool --> bool ] --> (DB driver) --> (DB column) bool|int(0/1)
* [ <-- string|int] <-- (DB driver) <-- (DB column) bool|int(0/1)
*/
class BooleanCast extends BaseCast
{
public static function get(mixed $value, array $params = []): bool
{
// For PostgreSQL
if ($value === 't') {
return true;
}
if ($value === 'f') {
return false;
}

return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
}
41 changes: 41 additions & 0 deletions system/DataCaster/Cast/CSVCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\DataCaster\Cast;

/**
* Class CSVCast
*
* (PHP) [array --> string] --> (DB driver) --> (DB column) string
* [ <-- string] <-- (DB driver) <-- (DB column) string
*/
class CSVCast extends BaseCast
{
public static function get(mixed $value, array $params = []): array
{
if (! is_string($value)) {
self::invalidTypeValueError($value);
}

return explode(',', $value);
}

public static function set(mixed $value, array $params = []): string
{
if (! is_array($value)) {
self::invalidTypeValueError($value);
}

return implode(',', $value);
}
}
37 changes: 37 additions & 0 deletions system/DataCaster/Cast/CastInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\DataCaster\Cast;

interface CastInterface
{
/**
* Takes a value from DataSource, returns its value for PHP.
*
* @param mixed $value Data from database driver
* @param list<string> $params Additional param
*
* @return mixed PHP native value
*/
public static function get(mixed $value, array $params = []): mixed;

/**
* Takes a PHP value, returns its value for DataSource.
*
* @param mixed $value PHP native value
* @param list<string> $params Additional param
*
* @return mixed Data to pass to database driver
*/
public static function set(mixed $value, array $params = []): mixed;
}
48 changes: 48 additions & 0 deletions system/DataCaster/Cast/DatetimeCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\DataCaster\Cast;

use CodeIgniter\I18n\Time;

/**
* Class DatetimeCast
*
* (PHP) [Time --> string] --> (DB driver) --> (DB column) datetime
* [ <-- string] <-- (DB driver) <-- (DB column) datetime
*/
class DatetimeCast extends BaseCast
{
public static function get(mixed $value, array $params = []): Time
{
if (! is_string($value)) {
self::invalidTypeValueError($value);
}

/**
* @see https://www.php.net/manual/en/datetimeimmutable.createfromformat.php#datetimeimmutable.createfromformat.parameters
*/
$format = $params[0] ?? 'Y-m-d H:i:s';

return Time::createFromFormat($format, $value);
}

public static function set(mixed $value, array $params = []): string
{
if (! $value instanceof Time) {
self::invalidTypeValueError($value);
}

return (string) $value;
}
}
Loading
Loading