Skip to content

[WIP] Apply Doctrine CS #968

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

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ vendor/
phpunit.xml
composer.lock
.idea/
phpcs.xml
.phpcs-cache
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"symfony/form": "^3.0|^4.0",
"symfony/filesystem": "^3.0|^4.0",
"symfony/expression-language": "^3.0|^4.0",
"phpunit/phpunit": "^7.1"
"phpunit/phpunit": "^7.1",
"doctrine/coding-standard": "^4.0"
},
"conflict": {
"hoa/core": "*",
Expand Down
39 changes: 39 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<ruleset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"
>
<arg name="basepath" value="."/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>
<arg value="nps"/>

<file>src/</file>
<file>tests/</file>

<rule ref="Doctrine">
<exclude name="Generic.Formatting.SpaceAfterNot"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousExceptionNaming"/>
<exclude name="SlevomatCodingStandard.ControlStructures.AssignmentInCondition.AssignmentInCondition"/>
<exclude name="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison.DisallowedYodaComparison"/>
<exclude name="SlevomatCodingStandard.ControlStructures.EarlyExit"/>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing">
<properties>
<property name="spacesCountBeforeColon" value="0"/>
</properties>
</rule>

<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<rule ref="Squiz.Classes.ClassFileName.NoMatch">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
</ruleset>
8 changes: 4 additions & 4 deletions src/AbstractVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace JMS\Serializer;

use function is_array;

/**
* @internal
*/
abstract class AbstractVisitor implements VisitorInterface
{
/**
* @var GraphNavigatorInterface
*/
/** @var GraphNavigatorInterface */
protected $navigator;

public function setNavigator(GraphNavigatorInterface $navigator): void
Expand All @@ -30,7 +30,7 @@ protected function getElementType(array $typeArray): ?array
return null;
}

if (isset($typeArray['params'][1]) && \is_array($typeArray['params'][1])) {
if (isset($typeArray['params'][1]) && is_array($typeArray['params'][1])) {
return $typeArray['params'][1];
} else {
return $typeArray['params'][0];
Expand Down
8 changes: 0 additions & 8 deletions src/Accessor/AccessorStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,15 @@
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\SerializationContext;

/**
* @author Asmir Mustafic <goetas@gmail.com>
*/
interface AccessorStrategyInterface
{
/**
* @param object $object
* @param PropertyMetadata $metadata
* @return mixed
*/
public function getValue(object $object, PropertyMetadata $metadata, SerializationContext $context);

/**
* @param object $object
* @param mixed $value
* @param PropertyMetadata $metadata
* @return void
*/
public function setValue(object $object, $value, PropertyMetadata $metadata, DeserializationContext $context): void;
}
19 changes: 7 additions & 12 deletions src/Accessor/DefaultAccessorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,18 @@
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Metadata\StaticPropertyMetadata;
use JMS\Serializer\SerializationContext;
use function sprintf;

/**
* @author Asmir Mustafic <goetas@gmail.com>
*/
final class DefaultAccessorStrategy implements AccessorStrategyInterface
{
private $readAccessors = [];
private $writeAccessors = [];
private $readAccessors = [];
private $writeAccessors = [];
private $propertyReflectionCache = [];

/**
* @var ExpressionEvaluatorInterface
*/
/** @var ExpressionEvaluatorInterface */
private $evaluator;

public function __construct(ExpressionEvaluatorInterface $evaluator = null)
public function __construct(?ExpressionEvaluatorInterface $evaluator = null)
{
$this->evaluator = $evaluator;
}
Expand All @@ -49,7 +45,6 @@ public function getValue(object $object, PropertyMetadata $metadata, Serializati
if (!isset($this->readAccessors[$metadata->class])) {
if ($metadata->forceReflectionAccess === true) {
$this->readAccessors[$metadata->class] = function ($o, $name) use ($metadata) {

$ref = $this->propertyReflectionCache[$metadata->class][$name] ?? null;
if ($ref === null) {
$ref = new \ReflectionProperty($metadata->class, $name);
Expand Down Expand Up @@ -81,7 +76,7 @@ public function setValue(object $object, $value, PropertyMetadata $metadata, Des
if (null === $metadata->setter) {
if (!isset($this->writeAccessors[$metadata->class])) {
if ($metadata->forceReflectionAccess === true) {
$this->writeAccessors[$metadata->class] = function ($o, $name, $value) use ($metadata) {
$this->writeAccessors[$metadata->class] = function ($o, $name, $value) use ($metadata): void {
$ref = $this->propertyReflectionCache[$metadata->class][$name] ?? null;
if ($ref === null) {
$ref = new \ReflectionProperty($metadata->class, $name);
Expand All @@ -92,7 +87,7 @@ public function setValue(object $object, $value, PropertyMetadata $metadata, Des
$ref->setValue($o, $value);
};
} else {
$this->writeAccessors[$metadata->class] = \Closure::bind(function ($o, $name, $value) {
$this->writeAccessors[$metadata->class] = \Closure::bind(function ($o, $name, $value): void {
$o->$name = $value;
}, null, $metadata->class);
}
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/AccessType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
/**
* @Annotation
* @Target({"CLASS", "PROPERTY"})
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
final class AccessType
{
Expand Down
10 changes: 2 additions & 8 deletions src/Annotation/Accessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@
/**
* @Annotation
* @Target("PROPERTY")
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
final class Accessor
{
/**
* @var string
*/
/** @var string */
public $getter;

/**
* @var string
*/
/** @var string */
public $setter;
}
5 changes: 1 addition & 4 deletions src/Annotation/AccessorOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*
* @Annotation
* @Target("CLASS")
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
final class AccessorOrder
{
Expand All @@ -19,8 +18,6 @@ final class AccessorOrder
*/
public $order;

/**
* @var array<string>
*/
/** @var array<string> */
public $custom = [];
}
2 changes: 1 addition & 1 deletion src/Annotation/Discriminator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Discriminator
/** @var string */
public $field = 'type';

/** @var boolean */
/** @var bool */
public $disabled = false;

/** @var string[] */
Expand Down
8 changes: 5 additions & 3 deletions src/Annotation/ExclusionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
namespace JMS\Serializer\Annotation;

use JMS\Serializer\Exception\RuntimeException;
use function is_string;
use function strtoupper;

/**
* @Annotation
* @Target("CLASS")
*/
final class ExclusionPolicy
{
const NONE = 'NONE';
const ALL = 'ALL';
public const NONE = 'NONE';
public const ALL = 'ALL';

public $policy;

public function __construct(array $values)
{
if (!\is_string($values['value'])) {
if (!is_string($values['value'])) {
throw new RuntimeException('"value" must be a string.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Annotation/MaxDepth.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class MaxDepth
{
/**
* @Required
* @var integer
* @var int
*/
public $depth;
}
1 change: 0 additions & 1 deletion src/Annotation/PostDeserialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*
* @Annotation
* @Target("METHOD")
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
final class PostDeserialize
{
Expand Down
1 change: 0 additions & 1 deletion src/Annotation/PreSerialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*
* @Annotation
* @Target("METHOD")
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
final class PreSerialize
{
Expand Down
4 changes: 1 addition & 3 deletions src/Annotation/ReadOnly.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
*/
final class ReadOnly
{
/**
* @var boolean
*/
/** @var bool */
public $readOnly = true;
}
4 changes: 3 additions & 1 deletion src/Annotation/SerializedName.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace JMS\Serializer\Annotation;

use JMS\Serializer\Exception\RuntimeException;
use function is_string;
use function sprintf;

/**
* @Annotation
Expand All @@ -16,7 +18,7 @@ final class SerializedName

public function __construct(array $values)
{
if (!isset($values['value']) || !\is_string($values['value'])) {
if (!isset($values['value']) || !is_string($values['value'])) {
throw new RuntimeException(sprintf('"value" must be a string.'));
}

Expand Down
5 changes: 2 additions & 3 deletions src/Annotation/VirtualProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace JMS\Serializer\Annotation;

use JMS\Serializer\Exception\InvalidArgumentException;
use function property_exists;
use function sprintf;

/**
* @Annotation
* @Target({"METHOD", "CLASS"})
*
* @author Alexander Klimenkov <alx.devel@gmail.com>
*/
final class VirtualProperty
{
Expand All @@ -33,4 +33,3 @@ public function __construct(array $data)
}
}
}

4 changes: 1 addition & 3 deletions src/Annotation/XmlAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
*/
final class XmlAttribute
{
/**
* @var string
*/
/** @var string */
public $namespace;
}
16 changes: 4 additions & 12 deletions src/Annotation/XmlCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@

abstract class XmlCollection
{
/**
* @var string
*/
/** @var string */
public $entry = 'entry';

/**
* @var boolean
*/
/** @var bool */
public $inline = false;

/**
* @var string
*/
/** @var string */
public $namespace;

/**
* @var boolean
*/
/** @var bool */
public $skipWhenEmpty = true;
}
12 changes: 3 additions & 9 deletions src/Annotation/XmlDiscriminator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@
*/
class XmlDiscriminator
{
/**
* @var boolean
*/
/** @var bool */
public $attribute = false;

/**
* @var boolean
*/
/** @var bool */
public $cdata = true;

/**
* @var string
*/
/** @var string */
public $namespace;
}
8 changes: 2 additions & 6 deletions src/Annotation/XmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
*/
final class XmlElement
{
/**
* @var boolean
*/
/** @var bool */
public $cdata = true;

/**
* @var string
*/
/** @var string */
public $namespace;
}
Loading