Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #15 from acelaya/feature/php-7.1
Browse files Browse the repository at this point in the history
Feature/php 7.1
  • Loading branch information
acelaya committed Feb 4, 2018
2 parents af952ab + af18db7 commit ac5babe
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 44 deletions.
10 changes: 2 additions & 8 deletions .travis.yml
Expand Up @@ -2,25 +2,19 @@ language: php

branches:
only:
- master
- develop
- /.*/

php:
- 5.6
- 7
- 7.1
- 7.2

before_script:
- composer self-update
- composer install --no-interaction
- if [[ $TRAVIS_PHP_VERSION = 7.1 ]] || [[ $TRAVIS_PHP_VERSION = 7.2 ]]; then composer global require --dev phpstan/phpstan:0.9.*; fi

script:
- mkdir build
- ./vendor/bin/phpcs --standard=PSR2 ./src ./tests -p
- if [[ $TRAVIS_PHP_VERSION = 7.1 ]] || [[ $TRAVIS_PHP_VERSION = 7.2 ]]; then ~/.composer/vendor/bin/phpstan analyse src/ --level=5; fi
- ./vendor/bin/phpunit -c tests/phpunit.xml --coverage-clover build/clover.xml
- composer ci

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,12 @@
## CHANGELOG

### 2.1.0

**Enhancements:**

* [16: Drop support for PHP 5.6 and 7.0](https://github.com/acelaya/doctrine-enum-type/issues/16)
* [17: Improve required coding standards](https://github.com/acelaya/doctrine-enum-type/issues/17)

### 2.0.3

**Bugs:**
Expand Down
32 changes: 23 additions & 9 deletions README.md
Expand Up @@ -24,36 +24,42 @@ Let's imagine we have this two enums.

```php
<?php
declare(strict_types=1);

namespace Acelaya\Enum;

use MyCLabs\Enum\Enum;

class Action extends Enum
{
const CREATE = 'create';
const READ = 'read';
const UPDATE = 'update';
const DELETE = 'delete';
public const CREATE = 'create';
public const READ = 'read';
public const UPDATE = 'update';
public const DELETE = 'delete';
}
```

```php
<?php
declare(strict_types=1);

namespace Acelaya\Enum;

use MyCLabs\Enum\Enum;

class Gender extends Enum
{
const MALE = 'male';
const FEMALE = 'female';
public const MALE = 'male';
public const FEMALE = 'female';
}
```

And this entity, with a column of each entity type.

```php
<?php
declare(strict_types=1);

namespace Acelaya\Entity;

use Acelaya\Enum\Action;
Expand Down Expand Up @@ -101,6 +107,8 @@ The column type of the action property is the FQCN of the `Action` enum, and the

```php
<?php
declare(strict_types=1);

// in bootstrapping code

// ...
Expand All @@ -122,6 +130,8 @@ Alternatively you can use the `Acelaya\Doctrine\Type\PhpEnumType::registerEnumTy

```php
<?php
declare(strict_types=1);

// ...

use Acelaya\Doctrine\Type\PhpEnumType;
Expand Down Expand Up @@ -154,18 +164,20 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla
If you want something more specific, like a MySQL enum, just extend `PhpEnumType` and overwrite the `getSQLDeclaration()` method with something like this.

```php
declare(strict_types=1);

namespace App\Type;

use Acelaya\Doctrine\Type\PhpEnumType;

class MyPhpEnumType extends PhpEnumType
{
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
{
$values = call_user_func([$this->enumClass, 'toArray']);
$values = \call_user_func([$this->enumClass, 'toArray']);
return sprintf(
'ENUM("%s") COMMENT "%s"',
implode('", "', $values),
\implode('", "', $values),
$this->getName()
);
}
Expand All @@ -176,6 +188,8 @@ Then remember to register the enums with your own class.

```php
<?php
declare(strict_types=1);

// ...

use Acelaya\Enum\Action;
Expand Down
28 changes: 22 additions & 6 deletions composer.json
Expand Up @@ -14,13 +14,15 @@
"enum"
],
"require": {
"php": "^5.6|^7.0",
"myclabs/php-enum": "^1.4",
"doctrine/dbal": "^2.4"
"php": "^7.1",
"doctrine/dbal": "^2.6",
"myclabs/php-enum": "^1.4"
},
"require-dev": {
"phpunit/phpunit": "^5.7|^6.0",
"squizlabs/php_codesniffer": "^2.0"
"phpstan/phpstan": "^0.9.2",
"phpunit/phpunit": "^7.0",
"slevomat/coding-standard": "^4.0",
"squizlabs/php_codesniffer": "^3.1"
},
"autoload": {
"psr-4": {
Expand All @@ -29,7 +31,21 @@
},
"autoload-dev": {
"psr-4": {
"Acelaya\\Test\\Doctrine\\": "tests"
"Acelaya\\Test\\Doctrine\\": "test"
}
},
"config": {
"sort-packages": true
},
"scripts": {
"ci": [
"@cs",
"@stan",
"@test"
],
"cs": "phpcs",
"cs-fix": "phpcbf",
"test": "phpunit --colors=always --coverage-clover build/clover.xml",
"stan": "phpstan analyse src/ --level=6"
}
}
46 changes: 46 additions & 0 deletions phpcs.xml
@@ -0,0 +1,46 @@
<?xml version="1.0"?>
<ruleset name="Coding standard specification">
<description>Coding standard specification</description>
<config name="installed_paths" value="../../slevomat/coding-standard"/>

<!-- display progress -->
<arg value="p"/>
<arg name="colors"/>

<!-- inherit rules from: -->
<rule ref="PSR2"/>
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<!-- Make sure string concatenations use 1 space between every element -->
<rule ref="Squiz.Strings.ConcatenationSpacing">
<properties>
<property name="spacing" value="1"/>
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
<properties>
<property name="ignoreBlankLines" value="false"/>
</properties>
</rule>
<rule ref="Squiz.Strings.DoubleQuoteUsage"/>
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="newlinesCountBetweenOpenTagAndDeclare" value="1"/>
<property name="spacesCountAroundEqualsSign" value="0"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
<properties>
<property name="searchAnnotations" value="true"/>
</properties>
</rule>
<!-- Force strict comparison with === or !== instead of == or != -->
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEqualOperators"/>
<rule ref="SlevomatCodingStandard.Arrays.TrailingArrayComma"/>
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses"/>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing"/>

<!-- Paths to check -->
<file>src</file>
<file>test</file>
</ruleset>
8 changes: 4 additions & 4 deletions tests/phpunit.xml → phpunit.xml
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../vendor/autoload.php"
<phpunit bootstrap="./vendor/autoload.php"
colors="true">

<testsuites>
<testsuite name="EnumType tests">
<directory>./</directory>
<directory>./test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>../src</directory>
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
5 changes: 3 additions & 2 deletions src/Exception/ExceptionInterface.php
@@ -1,7 +1,8 @@
<?php
declare(strict_types=1);

namespace Acelaya\Doctrine\Exception;

interface ExceptionInterface
interface ExceptionInterface extends \Throwable
{

}
2 changes: 2 additions & 0 deletions src/Exception/InvalidArgumentException.php
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Acelaya\Doctrine\Exception;

class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
Expand Down
14 changes: 8 additions & 6 deletions src/Type/PhpEnumType.php
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Acelaya\Doctrine\Type;

use Acelaya\Doctrine\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -53,13 +55,13 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
return null;
}

$isValid = call_user_func([$this->enumClass, 'isValid'], $value);
$isValid = \call_user_func([$this->enumClass, 'isValid'], $value);
if (! $isValid) {
throw new InvalidArgumentException(sprintf(
throw new InvalidArgumentException(\sprintf(
'The value "%s" is not valid for the enum "%s". Expected one of ["%s"]',
$value,
$this->enumClass,
implode('", "', call_user_func([$this->enumClass, 'toArray']))
\implode('", "', \call_user_func([$this->enumClass, 'toArray']))
));
}

Expand All @@ -82,8 +84,8 @@ public static function registerEnumType($typeNameOrEnumClass, $enumClass = null)
$typeName = $typeNameOrEnumClass;
$enumClass = $enumClass ?: $typeNameOrEnumClass;

if (! is_subclass_of($enumClass, Enum::class)) {
throw new InvalidArgumentException(sprintf(
if (! \is_subclass_of($enumClass, Enum::class)) {
throw new InvalidArgumentException(\sprintf(
'Provided enum class "%s" is not valid. Enums must extend "%s"',
$enumClass,
Enum::class
Expand All @@ -106,7 +108,7 @@ public static function registerEnumType($typeNameOrEnumClass, $enumClass = null)
public static function registerEnumTypes(array $types)
{
foreach ($types as $typeName => $enumClass) {
$typeName = is_string($typeName) ? $typeName : $enumClass;
$typeName = \is_string($typeName) ? $typeName : $enumClass;
static::registerEnumType($typeName, $enumClass);
}
}
Expand Down
10 changes: 6 additions & 4 deletions tests/Enum/Action.php → test/Enum/Action.php
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Acelaya\Test\Doctrine\Enum;

use MyCLabs\Enum\Enum;
Expand All @@ -15,8 +17,8 @@
*/
class Action extends Enum
{
const CREATE = 'create';
const READ = 'read';
const UPDATE = 'update';
const DELETE = 'delete';
public const CREATE = 'create';
public const READ = 'read';
public const UPDATE = 'update';
public const DELETE = 'delete';
}
6 changes: 4 additions & 2 deletions tests/Enum/Gender.php → test/Enum/Gender.php
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Acelaya\Test\Doctrine\Enum;

use MyCLabs\Enum\Enum;
Expand All @@ -13,6 +15,6 @@
*/
class Gender extends Enum
{
const MALE = 'male';
const FEMALE = 'female';
public const MALE = 'male';
public const FEMALE = 'female';
}
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Acelaya\Test\Doctrine\Type;

use Acelaya\Doctrine\Type\PhpEnumType;
Expand All @@ -11,11 +13,11 @@ class MyCustomEnumType extends PhpEnumType
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
$values = call_user_func([$this->enumClass, 'toArray']);
$values = \call_user_func([$this->enumClass, 'toArray']);

return sprintf(
return \sprintf(
'ENUM("%s") COMMENT "%s"',
implode('", "', $values),
\implode('", "', $values),
$this->getName()
);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Type/MyType.php → test/Type/MyType.php
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Acelaya\Test\Doctrine\Type;

use Acelaya\Doctrine\Type\PhpEnumType;
Expand Down
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Acelaya\Test\Doctrine\Type;

use Acelaya\Doctrine\Exception\InvalidArgumentException;
Expand Down

0 comments on commit ac5babe

Please sign in to comment.