Skip to content

Commit

Permalink
make phpstan happy
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Feb 9, 2024
1 parent ce0b50e commit 5573377
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 80 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ on:
push:
pull_request:
workflow_dispatch:
# Every Friday at 10:20am UTC
schedule:
- cron: '20 10 * * 5'

jobs:
ci:
name: CI
# Only run cron on the silverstripe account
if: (github.event_name == 'schedule' && github.repository_owner == 'lekoala') || (github.event_name != 'schedule')
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1
with:
simple_matrix: true
67 changes: 0 additions & 67 deletions .travis.yml

This file was deleted.

27 changes: 24 additions & 3 deletions src/DBUuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace LeKoala\Uuid;

use Ramsey\Uuid\Uuid;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBField;
use Tuupola\Base62Proxy;
use Ramsey\Uuid\Uuid;
use SilverStripe\Core\Convert;
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FormField;
use SilverStripe\ORM\FieldType\DBField;

/**
* A uuid field that stores Uuid in binary formats
Expand Down Expand Up @@ -56,10 +58,14 @@ public function filterExpression($type, $value)
$value = "%$value%";
}
$value = str_replace('-', '', $value);
/** @var string $value */
$value = Convert::raw2sql($value, true);
return "LOWER(HEX({$this->name})) $type $value";
}

/**
* @return void
*/
public function requireField()
{
// Use direct sql statement here
Expand Down Expand Up @@ -110,16 +116,27 @@ public function Base62()
return Base62Proxy::encode($this->value);
}

/**
* @param string $title
* @param array<mixed> $params
* @return FormField|null
*/
public function scaffoldFormField($title = null, $params = null)
{
return false;
return null;
}

public function nullValue()
{
return null;
}

/**
* @param mixed $value
* @param DataObject|array<string,mixed> $record
* @param boolean $markChanged
* @return $this
*/
public function setValue($value, $record = null, $markChanged = true)
{
if ($value && is_string($value) && strlen($value) > self::BINARY_LENGTH && Uuid::isValid($value)) {
Expand All @@ -128,6 +145,10 @@ public function setValue($value, $record = null, $markChanged = true)
return parent::setValue($value, $record, $markChanged);
}

/**
* @param string $value
* @return string|null
*/
public function prepValueForDB($value)
{
if (!$value) {
Expand Down
24 changes: 21 additions & 3 deletions src/UuidExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ class UuidExtension extends DataExtension
const UUID_STRING_FORMAT = 'string';
const UUID_BASE62_FORMAT = 'base62';

/**
* @var array<string,string>
*/
private static $db = [
self::UUID_FIELD => DBUuid::class,
];

/**
* @var array<string,mixed>
*/
private static $indexes = [
self::UUID_FIELD => true,
];
Expand Down Expand Up @@ -65,7 +71,7 @@ public function assignNewUuid($check = true)
* Get a record by its uuid
*
* @param string $class The class
* @param string $uuid The uuid value
* @param string $value The uuid value
* @param string $format Any UUID_XXXX_FORMAT constant or string
* @return DataObject|false The DataObject or false if no record is found or format invalid
*/
Expand Down Expand Up @@ -146,17 +152,29 @@ public function UuidSegment()
DB::prepared_query("UPDATE $table SET Uuid = ? WHERE ID = ?", [$uuid, $this->owner->ID]);
}
}
return $this->owner->dbObject(self::UUID_FIELD)->Base62();
/** @var DBUuid $dbObject */
$dbObject = $this->owner->dbObject(self::UUID_FIELD);
return $dbObject->Base62();
}

/**
* @param FieldList $fields
* @return void
*/
public function updateCMSFields(FieldList $fields)
{
if (DBUuid::config()->show_cms_field) {
$firstField = $fields->dataFieldNames()[0] ?? null;
$fields->addFieldToTab('Root.Main', ReadonlyField::create('UuidNice', 'Uuid', $this->owner->dbObject('Uuid')->Nice()), $firstField);
/** @var DBUuid $dbObject */
$dbObject = $this->owner->dbObject('Uuid');
$uuidField = ReadonlyField::create('UuidNice', 'Uuid', $dbObject->Nice());
$fields->addFieldToTab('Root.Main', $uuidField, $firstField);
}
}

/**
* @return void
*/
public function onBeforeWrite()
{
parent::onBeforeWrite();
Expand Down
9 changes: 9 additions & 0 deletions tests/Test_UuidModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ class Test_UuidModel extends DataObject implements TestOnly
{
use HasUuid;

/**
* @var string
*/
private static $table_name = 'UuidModel';

/**
* @var array<string,string>
*/
private static $db = [
'Title' => 'Varchar',
'UuidAlias' => 'Uuid',
];
/**
* @var array<clazss-string>
*/
private static $extensions = [
UuidExtension::class
];
Expand Down
7 changes: 5 additions & 2 deletions tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
*/
class UuidTest extends SapphireTest
{
/**
* @var array<class-string>
*/
protected static $extra_dataobjects = [
Test_UuidModel::class
];

public function testRecordGetsUuid()
public function testRecordGetsUuid(): void
{
$model = new Test_UuidModel;

Expand All @@ -39,7 +42,7 @@ public function testRecordGetsUuid()
$this->assertEquals($model->ID, $fetchedModel->ID);
}

public function testFormatting()
public function testFormatting(): void
{
$uuid = 'd84560c8-134f-11e6-a1e2-34363bd26dae';
$base62 = '6a630O1jrtMjCrQDyG3D3O';
Expand Down

0 comments on commit 5573377

Please sign in to comment.