Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bowlofsoup/NormalizerBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlOfSoup committed Nov 6, 2023
2 parents 7d91dc5 + fc967e0 commit 6f2128a
Show file tree
Hide file tree
Showing 21 changed files with 149 additions and 70 deletions.
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
/tests/coverage/
/build/
*.cache
var/
.idea/
/.idea
/var/**
!/var/**/
!/var/**/.gitkeep
/phpstan.neon
>>>>>>> fc967e0fc51238085fa8a2a2882bcdab42090dec
50 changes: 25 additions & 25 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
language: php
sudo: true
os: linux

cache:
directories:
- $HOME/.composer/cache

matrix:
include:
- php: 7.2
env: SYMFONY_VERSION=4.4.*
- php: 7.3
env: SYMFONY_VERSION=4.4.*
- php: 7.4
env: SYMFONY_VERSION=4.4.*
- php: 7.2
env: SYMFONY_VERSION=5.0.*
- php: 7.3
env: SYMFONY_VERSION=5.0.*
- php: 7.4
env: SYMFONY_VERSION=5.0.*
fast_finish: true
jobs:
include:
- php: 7.4
env: SYMFONY_VERSION=~4.4
- php: 7.4
env: SYMFONY_VERSION=~5.1
- dist: "jammy"
php: 8.2
env: SYMFONY_VERSION=~5.1
fast_finish: true

before_script:
- if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi;
- composer install
before_install:
- php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
- php composer-setup.php --version=1.10.22
- php -r "unlink('composer-setup.php');"
- sudo mv composer.phar /usr/local/bin/composer

install:
- if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi;
- COMPOSER_MEMORY_LIMIT=-1 travis_retry composer install
- mkdir -p tests/coverage

script:
- mkdir -p tests/coverage
- php vendor/bin/phpunit
- vendor/bin/rector process --dry-run --no-progress-bar --ansi
- vendor/bin/phpstan analyze --no-progress --ansi
- php vendor/bin/phpunit

after_script:
- php vendor/bin/coveralls -v

before_install:
- echo "memory_limit=2048M" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- php vendor/bin/coveralls -v
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
"doctrine/collections": "1.*",
"php-coveralls/php-coveralls": "^2.0",
"phpunit/phpunit": "^8.5",
"friendsofphp/php-cs-fixer": "^2.10"
"friendsofphp/php-cs-fixer": "^2.10",
"rector/rector": "^0.18.6",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-mockery": "^1.1",
"phpstan/phpstan-symfony": "^1.3"
},
"autoload": {
"psr-4": { "BowlOfSoup\\NormalizerBundle\\": "src/" },
Expand All @@ -41,6 +45,10 @@
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
},
"symfony": {
"allow-contrib": false,
"require": "4.4.* || 5.1.*"
}
}
}
4 changes: 2 additions & 2 deletions src/Resources/config/services.xml → config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<services>
<defaults autowire="true" autoconfigure="true"/>

<prototype namespace="BowlOfSoup\NormalizerBundle\" resource="../../../src/*" exclude="../../../src/{Annotation,DependencyInjection,EventListener,Exception,Model}"/>
<prototype namespace="BowlOfSoup\NormalizerBundle\" resource="../src/*" exclude="../src/{Annotation,DependencyInjection,EventListener,Exception,Model}"/>

<service class="BowlOfSoup\NormalizerBundle\EventListener\RegisterAnnotationsListener" id="bos.event_listener.register_annotations">
<tag name="kernel.event_subscriber"/>
Expand All @@ -20,4 +20,4 @@

<service class="BowlOfSoup\NormalizerBundle\Service\Normalizer" id="bos.normalizer" autowire="true" />
</services>
</container>
</container>
11 changes: 11 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
parameters:
level: 1
paths:
- 'src/'
- 'tests/'

tmpDir: var/cache/phpstan

includes:
- vendor/phpstan/phpstan-mockery/extension.neon
- vendor/phpstan/phpstan-symfony/extension.neon
30 changes: 30 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Symfony\Set\SymfonyLevelSetList;

return static function (RectorConfig $rectorConfig): void
{
$rectorConfig->paths([
__DIR__,
]);

$rectorConfig->skip([
__DIR__ . '/var',
__DIR__ . '/vendor',
]);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_72,
SymfonyLevelSetList::UP_TO_SYMFONY_51,
]);

$rectorConfig->importNames(true, false);
$rectorConfig->importShortClasses(false);

$rectorConfig->cacheDirectory(__DIR__ . '/var/cache/rector');
$rectorConfig->containerCacheDirectory(__DIR__ . '/var');
};
4 changes: 2 additions & 2 deletions src/Annotation/Normalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public function __construct(array $properties)
{
foreach ($properties as $propertyName => $propertyValue) {
if (!array_key_exists($propertyName, $this->supportedProperties)) {
throw new \InvalidArgumentException(sprintf(static::EXCEPTION_UNKNOWN_PROPERTY, $propertyName, __CLASS__));
throw new \InvalidArgumentException(sprintf(static::EXCEPTION_UNKNOWN_PROPERTY, $propertyName, self::class));
}

if ($this->validateProperties($propertyValue, $propertyName, $this->supportedProperties[$propertyName], __CLASS__)) {
if ($this->validateProperties($propertyValue, $propertyName, $this->supportedProperties[$propertyName], self::class)) {
$this->$propertyName = $propertyValue;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Annotation/Serialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public function __construct(array $properties)
{
foreach ($properties as $propertyName => $propertyValue) {
if (!array_key_exists($propertyName, $this->supportedProperties)) {
throw new \InvalidArgumentException(sprintf(static::EXCEPTION_UNKNOWN_PROPERTY, $propertyName, __CLASS__));
throw new \InvalidArgumentException(sprintf(static::EXCEPTION_UNKNOWN_PROPERTY, $propertyName, self::class));
}

if ($this->validateProperties($propertyValue, $propertyName, $this->supportedProperties[$propertyName], __CLASS__)) {
if ($this->validateProperties($propertyValue, $propertyName, $this->supportedProperties[$propertyName], self::class)) {
$this->$propertyName = $propertyValue;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Annotation/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public function __construct(array $properties)
{
foreach ($properties as $propertyName => $propertyValue) {
if (!array_key_exists($propertyName, $this->supportedProperties)) {
throw new \InvalidArgumentException(sprintf(static::EXCEPTION_UNKNOWN_PROPERTY, $propertyName, __CLASS__));
throw new \InvalidArgumentException(sprintf(static::EXCEPTION_UNKNOWN_PROPERTY, $propertyName, self::class));
}

if ($this->validateProperties($propertyValue, $propertyName, $this->supportedProperties[$propertyName], __CLASS__)) {
if ($this->validateProperties($propertyValue, $propertyName, $this->supportedProperties[$propertyName], self::class)) {
$this->$propertyName = $propertyValue;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/BowlOfSoupNormalizerBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

class BowlOfSoupNormalizerBundle extends Bundle
{
public function getPath(): string
{
return \dirname(__DIR__);
}
}
4 changes: 2 additions & 2 deletions src/DependencyInjection/BowlOfSoupNormalizerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class BowlOfSoupNormalizerExtension extends Extension
Expand All @@ -19,7 +19,7 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter('bowl_of_soup_normalizer.register_annotations', $parameters['register_annotations']);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$loader->load('services.xml');
}
}
16 changes: 3 additions & 13 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,10 @@ public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('bowl_of_soup_normalizer');

if (is_callable([$treeBuilder, 'getRootNode'])) {
$treeBuilder
->getRootNode()
->children()
$treeBuilder->getRootNode()
->children()
->booleanNode('register_annotations')->defaultValue(false)->end()
->end();
}
if (is_callable([$treeBuilder, 'root'])) {
$treeBuilder
->root('bowl_of_soup_normalizer')
->children()
->booleanNode('register_annotations')->defaultValue(false)->end()
->end();
}
->end();

return $treeBuilder;
}
Expand Down
26 changes: 22 additions & 4 deletions src/Service/Extractor/MethodExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class MethodExtractor

/**
* @param object|string $object
*
* @throws \ReflectionException
*/
public function getMethods($object): array
{
Expand All @@ -22,10 +20,30 @@ public function getMethods($object): array

$reflectedClass = new \ReflectionClass($object);

return $reflectedClass->getMethods(
$methods = $reflectedClass->getMethods(
\ReflectionMethod::IS_PUBLIC |
\ReflectionMethod::IS_PROTECTED |
\ReflectionMethod::IS_PRIVATE
\ReflectionMethod::IS_PRIVATE |
\ReflectionMethod::IS_STATIC
);

$parentClass = $reflectedClass->getParentClass();
if ($parentClass) {
// Since PHP >= 8 it will not get the private methods of an abstract, get it explicitly.
$parentPrivateMethods = $parentClass->getMethods(\ReflectionMethod::IS_PRIVATE);
$methods = array_merge($methods, $parentPrivateMethods);

// Make sure methods are unique (will happen if PHP < 8)
$uniqueMethods = [];
foreach ($methods as $key => $method) {
$id = $method->class . ':' . $method->name;
if (isset($uniqueMethods[$id])) {
unset($methods[$key]);
}
$uniqueMethods[$id] = true;
}
}

return $methods;
}
}
2 changes: 1 addition & 1 deletion src/Service/ObjectHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function getObjectIdentifier($object)
{
$objectId = self::getObjectId($object);

return null === $objectId ? static::hashObject($object) : $objectId;
return $objectId ?? static::hashObject($object);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/ArraySubset.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @codeCoverageIgnore
*/
class ArraySubset extends Constraint
final class ArraySubset extends Constraint
{
/**
* @var iterable
Expand Down
17 changes: 14 additions & 3 deletions tests/NormalizerTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,27 @@ public function getNormalizer(): Normalizer
$annotationExtractor = $this->annotationExtractor ?? new AnnotationExtractor();

/** @var \PHPUnit\Framework\MockObject\MockBuilder $translationMockBuilder */
$translationMockBuilder = $this->getMockBuilder(TranslatorInterface::class);
$translationMockBuilder->disableOriginalConstructor();
$translationMockBuilder = $this->getMockBuilder(TranslatorInterface::class)
->disableOriginalConstructor();

$mockMethodsTranslator = ['trans'];
if (method_exists(TranslatorInterface::class, 'getLocale')) { // Support for Symfony < 6
$mockMethodsTranslator[] = 'getLocale';
}

$this->translator = $translationMockBuilder
->onlyMethods(['trans'])
->onlyMethods($mockMethodsTranslator)
->getMock();
$this->translator
->method('trans')
->willReturn('translatedValue');

if (method_exists(TranslatorInterface::class, 'getLocale')) { // Support for Symfony < 6
$this->translator
->method('getLocale')
->willReturn('en');
}

$propertyNormalizer = $this->propertyNormalizer ?? new PropertyNormalizer($classExtractor, $this->translator, $annotationExtractor, $propertyExtractor);
$methodNormalizer = $this->methodNormalizer ?? new MethodNormalizer($classExtractor, $this->translator, $annotationExtractor, $methodExtractor);

Expand Down
4 changes: 2 additions & 2 deletions tests/Service/Extractor/AnnotationExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testExtractClassAnnotation(): void

/** @var \Doctrine\Common\Annotations\AnnotationReader $mockAnnotationReader */
$mockAnnotationReader = $this
->getMockBuilder('Doctrine\Common\Annotations\AnnotationReader')
->getMockBuilder(AnnotationReader::class)
->disableOriginalConstructor()
->setMethods(['getClassAnnotations'])
->getMock();
Expand All @@ -55,7 +55,7 @@ public function testExtractClassAnnotationNoClassGiven(): void
{
/** @var \Doctrine\Common\Annotations\AnnotationReader $mockAnnotationReader */
$mockAnnotationReader = $this
->getMockBuilder('Doctrine\Common\Annotations\AnnotationReader')
->getMockBuilder(AnnotationReader::class)
->disableOriginalConstructor()
->getMock();

Expand Down
Loading

0 comments on commit 6f2128a

Please sign in to comment.