Skip to content

Commit

Permalink
Format code with PHP-CS-Fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorejb committed Oct 23, 2024
1 parent f5be4ac commit d39188e
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 28 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ jobs:

- name: Run PHPUnit
run: composer test-mysql

- name: Check Formatting
run: vendor/bin/php-cs-fixer fix -v --dry-run --stop-on-violation --using-cache=no
if: ${{ matrix.php == '8.3' }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/composer.lock
/test/src/LocalConfig.php
.phpunit.result.cache
.php-cs-fixer.cache
58 changes: 58 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

$finder = PhpCsFixer\Finder::create()
->path([
'src/',
'test/',
])
->ignoreVCSIgnored(true)
->append([__FILE__])
->in(__DIR__);

$config = new PhpCsFixer\Config();
return $config
->setRules([
'@PER-CS' => true,
'align_multiline_comment' => true,
'array_syntax' => true,
'binary_operator_spaces' => true,
'class_attributes_separation' => ['elements' => ['method' => 'one']],
'class_reference_name_casing' => true,
'clean_namespace' => true,
'combine_consecutive_unsets' => true,
'declare_parentheses' => true,
'integer_literal_case' => true,
'lambda_not_used_import' => true,
'linebreak_after_opening_tag' => true,
'method_chaining_indentation' => true,
'multiline_comment_opening_closing' => true,
'native_function_casing' => true,
'no_alternative_syntax' => true,
'no_blank_lines_after_phpdoc' => true,
'no_empty_comment' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_blank_lines' => true,
'no_spaces_around_offset' => true,
'no_superfluous_phpdoc_tags' => true,
'no_unneeded_control_parentheses' => true,
'no_unused_imports' => true,
'no_useless_return' => true,
'no_whitespace_before_comma_in_array' => true,
'object_operator_without_whitespace' => true,
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'phpdoc_indent' => true,
'phpdoc_no_empty_return' => true,
'phpdoc_single_line_var_spacing' => true,
'return_assignment' => true,
'semicolon_after_instruction' => true,
'single_class_element_per_statement' => true,
'single_space_around_construct' => true,
'space_after_semicolon' => true,
'standardize_not_equals' => true,
'trim_array_spaces' => true,
'type_declaration_spaces' => true,
'types_spaces' => true,
'whitespace_after_comma_in_array' => ['ensure_single_space' => true],
])
->setFinder($finder);
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"theodorejb/peachy-sql": "^6.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.64",
"phpunit/phpunit": "^10.5",
"psalm/plugin-phpunit": "^0.19",
"vimeo/psalm": "^5.26"
Expand All @@ -32,6 +33,7 @@
},
"scripts": {
"analyze": "psalm",
"cs-fix": "php-cs-fixer fix -v",
"test": "phpunit",
"test-mssql": "phpunit --exclude-group mysql",
"test-mysql": "phpunit --exclude-group mssql",
Expand Down
4 changes: 2 additions & 2 deletions src/Entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function getEntitiesByIds(array $ids, array $fields = [], array $sort = [
public function getEntities(array $filter = [], array $fields = [], array $sort = [], int $offset = 0, int $limit = 0): array
{
$processedFilter = $this->processFilter($filter);
$selectMap = Helpers::propMapToSelectMap($this->fullPropMap);;
$selectMap = Helpers::propMapToSelectMap($this->fullPropMap);

if ($sort === []) {
$sort = $this->getDefaultSort();
Expand All @@ -287,7 +287,7 @@ public function getEntities(array $filter = [], array $fields = [], array $sort
public function countEntities(array $filter = []): int
{
$processedFilter = $this->processFilter($filter);
$selectMap = Helpers::propMapToSelectMap($this->fullPropMap);;
$selectMap = Helpers::propMapToSelectMap($this->fullPropMap);

$prop = new Prop('count', 'COUNT(*)', false, true, 'count');
$queryOptions = new QueryOptions($processedFilter, $filter, [], [$prop]);
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public static function mapRows(\Generator $rows, array $fieldProps): array

foreach ($aliasMap as $colName => $prop) {
if ($prop->getValue !== null) {
/** @var mixed $value */
/** @psalm-suppress MixedAssignment */
$value = ($prop->getValue)($row);
} else {
/** @var mixed $value */
/** @psalm-suppress MixedAssignment */
$value = $row[$colName];

if ($prop->type !== null) {
Expand Down
2 changes: 1 addition & 1 deletion src/Prop.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
public readonly DateTimeZone|null|false $timeZone = false,
public readonly ?Closure $getValue = null,
public readonly array $dependsOn = [],
bool $output = true
bool $output = true,
) {
$this->output = $output;
$this->map = explode('.', $name);
Expand Down
2 changes: 1 addition & 1 deletion src/QueryOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(
public readonly array $filter,
public readonly array $originalFilter,
public readonly array $sort,
public readonly array $fieldProps
public readonly array $fieldProps,
) {}

public function getColumns(): string
Expand Down
2 changes: 1 addition & 1 deletion src/RouteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function search(string $class, int $defaultLimit = 25, int $maxLimit = 10
} elseif (filter_var($value, FILTER_VALIDATE_INT) === false) {
throw new HttpException("Parameter '{$param}' must be an integer", StatusCode::BAD_REQUEST);
} else {
$params[$param] = (int)$value;
$params[$param] = (int) $value;
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PeachySQL\PeachySql;
use PHPUnit\Framework\TestCase;
use theodorejb\Phaster\Test\src\{Users, LegacyUsers, ModernUsers};
use theodorejb\Phaster\Test\src\{LegacyUsers, ModernUsers, Users};

abstract class DbTestCase extends TestCase
{
Expand Down
14 changes: 7 additions & 7 deletions test/EntitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ public function testInvalidMap(): void
$duplicateColumns = [
'prop1' => 'TestCol',
'prop2' => [
'subProp' => 'TestCol'
]
'subProp' => 'TestCol',
],
];

$properties = [
'prop1' => 'Hello',
'prop2' => [
'subProp' => 'World'
]
'subProp' => 'World',
],
];

try {
Expand All @@ -145,15 +145,15 @@ public function testInvalidPropertiesToColumns(): void
}

try {
$badData = [ 'client' => null ];
$badData = ['client' => null];
Entities::propertiesToColumns($this->propertyMap, $badData);
$this->fail('Failed to throw exception for null property');
} catch (HttpException $e) {
$this->assertSame("Expected client property to be an object, got null", $e->getMessage());
}

try {
$badData = [ 'client' => 'some string' ];
$badData = ['client' => 'some string'];
Entities::propertiesToColumns($this->propertyMap, $badData);
$this->fail('Failed to throw exception for invalid string property');
} catch (\Exception $e) {
Expand All @@ -163,7 +163,7 @@ public function testInvalidPropertiesToColumns(): void

public function testPartialPropertiesToColumns(): void
{
$map = [
$map = [
'name' => 'UserName',
'client' => [
'id' => 'ClientID',
Expand Down
24 changes: 12 additions & 12 deletions test/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PHPUnit\Framework\TestCase;
use Teapot\HttpException;
use theodorejb\Phaster\{Prop, Helpers};
use theodorejb\Phaster\{Helpers, Prop};

class HelpersTest extends TestCase
{
Expand Down Expand Up @@ -71,8 +71,8 @@ public function testMapRows(): void
// test mapping all fields with nullable client.type group
$generator = function (): \Generator {
yield ['UserID' => 5, 'UserName' => 'theodoreb', 'ClientID' => 1, 'Disabled' => 0, 'TypeID' => 2, 'DateCreatedUTC' => '2019-02-18 09:01:35'];
yield ['UserID' => 42, 'UserName' => 'jsmith', 'ClientID' => 2, 'Disabled' => 1, 'TypeID' => null, 'DateCreatedUTC' => '2018-05-20 23:22:40'];
yield ['UserID' => 1, 'UserName' => 'test', 'ClientID' => null, 'Disabled' => null, 'TypeID' => null, 'DateCreatedUTC' => null];
yield ['UserID' => 42, 'UserName' => 'jsmith', 'ClientID' => 2, 'Disabled' => 1, 'TypeID' => null, 'DateCreatedUTC' => '2018-05-20 23:22:40'];
yield ['UserID' => 1, 'UserName' => 'test', 'ClientID' => null, 'Disabled' => null, 'TypeID' => null, 'DateCreatedUTC' => null];
};

$expected = [
Expand All @@ -88,7 +88,7 @@ public function testMapRows(): void
$generator = function (): \Generator {
yield ['UserID' => 5, 'UserName' => 'theodoreb', 'DateCreatedUTC' => '2019-02-18 09:01:35'];
yield ['UserID' => 42, 'UserName' => 'jsmith', 'DateCreatedUTC' => '2018-05-20 23:22:40'];
yield ['UserID' => 1, 'UserName' => 'test', 'DateCreatedUTC' => null];
yield ['UserID' => 1, 'UserName' => 'test', 'DateCreatedUTC' => null];
};

$expected = [
Expand All @@ -103,8 +103,8 @@ public function testMapRows(): void
// test nullable client group when selecting client.isDisabled child property
$generator = function (): \Generator {
yield ['UserID' => 5, 'UserName' => 'theodoreb', 'ClientID' => 1, 'Disabled' => 0, 'DateCreatedUTC' => '2019-02-18 09:01:35'];
yield ['UserID' => 42, 'UserName' => 'jsmith', 'ClientID' => 2, 'Disabled' => 1, 'DateCreatedUTC' => '2018-05-20 23:22:40'];
yield ['UserID' => 1, 'UserName' => 'test', 'ClientID' => null, 'Disabled' => null, 'DateCreatedUTC' => null];
yield ['UserID' => 42, 'UserName' => 'jsmith', 'ClientID' => 2, 'Disabled' => 1, 'DateCreatedUTC' => '2018-05-20 23:22:40'];
yield ['UserID' => 1, 'UserName' => 'test', 'ClientID' => null, 'Disabled' => null, 'DateCreatedUTC' => null];
};

$expected = [
Expand All @@ -121,8 +121,8 @@ public function testMapRows(): void
// test nullable client group when selecting client.type.id grandchild property
$generator = function (): \Generator {
yield ['UserID' => 5, 'UserName' => 'theodoreb', 'ClientID' => 1, 'TypeID' => 2];
yield ['UserID' => 42, 'UserName' => 'jsmith', 'ClientID' => 2, 'TypeID' => null];
yield ['UserID' => 1, 'UserName' => 'test', 'ClientID' => null, 'TypeID' => null];
yield ['UserID' => 42, 'UserName' => 'jsmith', 'ClientID' => 2, 'TypeID' => null];
yield ['UserID' => 1, 'UserName' => 'test', 'ClientID' => null, 'TypeID' => null];
};

$expected = [
Expand Down Expand Up @@ -282,7 +282,7 @@ public function testPropMapToAliasMap(): void

public function testAllPropertiesToColumns(): void
{
$map = [
$map = [
'name' => 'UserName',
'client' => [
'id' => 'ClientID',
Expand All @@ -302,7 +302,7 @@ public function testAllPropertiesToColumns(): void
}

try {
$missingClientId = [
$missingClientId = [
'name' => 'Test Name',
'client' => [],
];
Expand All @@ -314,7 +314,7 @@ public function testAllPropertiesToColumns(): void
}

try {
$missingGroupTypeId = [
$missingGroupTypeId = [
'name' => 'Test Name',
'client' => ['id' => null],
'group' => ['type' => []],
Expand All @@ -338,7 +338,7 @@ public function testAllPropertiesToColumns(): void
$expected = [
'UserName' => 'Test Name',
'ClientID' => null,
'GroupTypeID' => 234
'GroupTypeID' => 234,
];

$this->assertSame($expected, Helpers::allPropertiesToColumns($map, $valid));
Expand Down
2 changes: 1 addition & 1 deletion test/src/LegacyUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace theodorejb\Phaster\Test\src;

use theodorejb\Phaster\{Entities, QueryOptions};
use PeachySQL\QueryBuilder\SqlParams;
use theodorejb\Phaster\{Entities, QueryOptions};

class LegacyUsers extends Entities
{
Expand Down

0 comments on commit d39188e

Please sign in to comment.