Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 0 additions & 2 deletions .github/workflows/code-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ jobs:
strategy:
matrix:
php:
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/vendor
/.phpunit.cache/
/vendor/
/composer.lock
/.phpunit.result.cache
2 changes: 1 addition & 1 deletion .runConfigurations/All unit tests.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="All unit tests" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml.dist" directory="$PROJECT_DIR$/tests" scope="XML" options="--verbose" />
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml.dist" directory="$PROJECT_DIR$/tests" scope="XML" />
<method v="2" />
</configuration>
</component>
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Removed
- **BC break**: Removed support for PHP versions <= v8.0 as they are no longer
[actively supported](https://php.net/supported-versions.php) by the PHP project.

## [1.0.1] - 2025-02-08
### Changed
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
}
],
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.1",
"phlib/db": "^2",
"symfony/process": "^5 || ^6 || ^7"
"symfony/process": "^6 || ^7"
},
"require-dev": {
"phpunit/phpunit": "^9",
"phpunit/phpunit": "^10",
"symplify/easy-coding-standard": "^12"
},
"autoload": {
Expand Down
15 changes: 15 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,19 @@
[
'closure_fn_spacing' => 'none',
],
)

/*
* Rule from PER Coding Style 2.6:
* "If the list is split across multiple lines, then the last item MUST have a trailing comma."
*/
->withConfiguredRule(
\PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer::class,
[
'elements' => [
\PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS,
\PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer::ELEMENTS_ARRAYS,
\PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer::ELEMENTS_PARAMETERS,
],
],
);
7 changes: 4 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
beStrictAboutOutputDuringTests="true"
cacheDirectory=".phpunit.cache"
colors="true"
>
<testsuites>
<testsuite name="Phlib Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</source>
</phpunit>
22 changes: 8 additions & 14 deletions src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@

use Phlib\Db\SqlFragment;

class Column
class Column implements \Stringable
{
use FormatterTrait;

private const POSITION_FIRST = 'FIRST';

private const POSITION_AFTER = 'AFTER';

private string $name;

private string $type;

private string $newName;

private bool $unsigned;
Expand All @@ -28,20 +24,18 @@ class Column

private bool $nullable = true;

/**
* @var string|SqlFragment
*/
private $default;
private string|SqlFragment $default;

private bool $auto;

private array $position;

public function __construct(Formatter $formatter, string $name, string $type)
{
public function __construct(
Formatter $formatter,
private readonly string $name,
private string $type,
) {
$this->formatter = $formatter;
$this->name = $name;
$this->type = $type;
}

public function getName(): string
Expand Down Expand Up @@ -150,7 +144,7 @@ public function toSql(): string
if (!$value instanceof SqlFragment) {
$value = $this->quoteValue($value);
}
$definition[] = 'DEFAULT ' . (string)$value;
$definition[] = 'DEFAULT ' . $value;
}

if (isset($this->auto) && $this->auto === true) {
Expand Down
8 changes: 3 additions & 5 deletions src/Formatter/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

class Db implements Formatter
{
private Adapter $db;

private NameMapper $nameMapper;

public function __construct(Adapter $db)
{
$this->db = $db;
public function __construct(
private readonly Adapter $db,
) {
}

public function setNameMapper(NameMapper $nameMapper): void
Expand Down
1 change: 0 additions & 1 deletion src/Formatter/TestFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class TestFake implements Formatter
{
public function setNameMapper(NameMapper $nameMapper): void
{
return;
}

public function tableIdentifier(string $tableIdentifier): string
Expand Down
2 changes: 1 addition & 1 deletion src/FormatterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait FormatterTrait
{
protected Formatter $formatter;
protected readonly Formatter $formatter;

protected function tableIdentifier(string $tableIdentifer): string
{
Expand Down
9 changes: 3 additions & 6 deletions src/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

namespace Phlib\SchemaChange;

class Index
class Index implements \Stringable
{
use FormatterTrait;

private string $tableName;

private string $name;

private array $columns;
Expand All @@ -18,11 +16,10 @@ class Index

public function __construct(
Formatter $formatter,
string $tableName,
string ...$columns
private readonly string $tableName,
string ...$columns,
) {
$this->formatter = $formatter;
$this->tableName = $tableName;
$this->columns = $columns;
}

Expand Down
19 changes: 8 additions & 11 deletions src/OnlineChangeRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@

class OnlineChangeRunner
{
private string $binPath;

/**
* @var \Closure {
* @return Process
* }
* @var \Closure():Process
*/
private \Closure $processFactory;
private readonly \Closure $processFactory;

public function __construct(string $binPath, ?\Closure $processFactory = null)
{
$this->binPath = $binPath;
public function __construct(
private readonly string $binPath,
?\Closure $processFactory = null,
) {
$this->processFactory = $processFactory ?? static function (...$args): Process {
return new Process(...$args);
};
Expand All @@ -32,7 +29,7 @@ public function execute(array $dbConfig, OnlineChange $onlineChange): void
$cmd = array_merge(
[$this->binPath, $this->buildDsn($dbConfig, $onlineChange->getName())],
$this->getOptions($onlineChange),
['--alter', $onlineChange->toOnlineAlter()]
['--alter', $onlineChange->toOnlineAlter()],
);
$process = $this->getProcess($cmd);

Expand Down Expand Up @@ -80,7 +77,7 @@ private function buildDsn(array $dbConfig, string $tableName): string
return $dsn;
}

private function getProcess(...$args): Process
private function getProcess(array ...$args): Process
{
return ($this->processFactory)(...$args);
}
Expand Down
15 changes: 6 additions & 9 deletions src/SchemaChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@

class SchemaChange
{
private Adapter $db;
private readonly Formatter $formatter;

private Formatter $formatter;

private ?OnlineChangeRunner $onlineChangeRunner;

public function __construct(Adapter $db, OnlineChangeRunner $onlineChangeRunner = null, Formatter $formatter = null)
{
$this->db = $db;
public function __construct(
private readonly Adapter $db,
private readonly ?OnlineChangeRunner $onlineChangeRunner = null,
Formatter $formatter = null,
) {
$this->formatter = $formatter ?? new DbFormatter($this->db);
$this->onlineChangeRunner = $onlineChangeRunner;
}

public function mapNames(NameMapper $nameMapper): void
Expand Down
9 changes: 4 additions & 5 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ abstract class Table implements Change
{
use FormatterTrait;

protected string $table;

public function __construct(Formatter $formatter, string $table)
{
public function __construct(
Formatter $formatter,
protected readonly string $table,
) {
$this->formatter = $formatter;
$this->table = $table;
}
}
10 changes: 4 additions & 6 deletions src/Table/Alter.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ public function toSql(): string
$tableName = $this->tableIdentifier($this->table);
$ddl = "ALTER TABLE {$tableName}\n";

$ddl .= $this->getCmds();

return $ddl;
return $ddl . $this->getCmds();
}

public function toOnlineAlter(): string
Expand All @@ -117,7 +115,7 @@ private function getCmds(): string
}
// add columns
foreach ($this->addColumns as $column) {
$cmds[] = 'ADD ' . (string)$column;
$cmds[] = 'ADD ' . $column;
}
// drop column defaults
foreach ($this->dropDefaultColumns as $columnName) {
Expand All @@ -127,7 +125,7 @@ private function getCmds(): string
// change columns
foreach ($this->changeColumns as $column) {
$columnName = $this->quoteIdentifier($column->getName());
$cmds[] = 'CHANGE COLUMN ' . $columnName . ' ' . (string)$column;
$cmds[] = 'CHANGE COLUMN ' . $columnName . ' ' . $column;
}
// remove fields
foreach ($this->removeColumns as $column) {
Expand All @@ -143,7 +141,7 @@ private function getCmds(): string
}
// add indexes (key)
foreach ($this->addIndexes as $index) {
$cmds[] = 'ADD ' . (string)$index;
$cmds[] = 'ADD ' . $index;
}
// remove indexes (key)
foreach ($this->removeIndexes as $index) {
Expand Down
12 changes: 3 additions & 9 deletions tests/Formatter/DbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@

class DbTest extends TestCase
{
/**
* @var Adapter|MockObject
*/
private $db;

/**
* @var Adapter\QuoteHandler|MockObject
*/
private $quoter;
private Adapter&MockObject $db;

private Adapter\QuoteHandler&MockObject $quoter;

protected function setUp(): void
{
Expand Down