Skip to content

Commit

Permalink
Update main component to version 9 (#45)
Browse files Browse the repository at this point in the history
* fix code style

* refactor tests
  • Loading branch information
marvin255 authored Apr 10, 2021
1 parent b8ef5e7 commit bac0273
Show file tree
Hide file tree
Showing 56 changed files with 650 additions and 567 deletions.
17 changes: 17 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ $rules = [
'phpdoc_no_empty_return' => false,
'no_superfluous_phpdoc_tags' => false,
'single_line_throw' => false,
'array_indentation' => true,
'declare_strict_types' => true,
'void_return' => true,
'non_printable_character' => true,
'modernize_types_casting' => true,
'ordered_interfaces' => ['order' => 'alpha', 'direction' => 'ascend'],
'date_time_immutable' => true,
'native_constant_invocation' => true,
'combine_nested_dirname' => true,
'native_function_invocation' => ['include' => ['@compiler_optimized'], 'scope' => 'namespaced', 'strict' => true],
'php_unit_construct' => true,
'php_unit_dedicate_assert' => true,
'php_unit_expectation' => true,
'php_unit_internal_class' => true,
'php_unit_mock_short_will_return' => true,
'php_unit_strict' => true,
'strict_comparison' => true,
];

return PhpCsFixer\Config::create()->setRules($rules)->setFinder($finder);
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ user_id := $(shell id -u)
docker_compose_bin := $(shell command -v docker-compose 2> /dev/null) --file "docker/docker-compose.yml"
php_container_bin := $(docker_compose_bin) run --rm -u "$(user_id)" "php"

.PHONY : help build install shell fixer test coverage entites
.PHONY : help build install shell fixer test coverage entities
.DEFAULT_GOAL := build

# --- [ Development tasks ] -------------------------------------------------------------------------------------------
Expand Down
18 changes: 13 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"require": {
"php": ">=7.4",
"liquetsoft/fias-component": "^8.0",
"liquetsoft/fias-component": "^9.0",
"elasticsearch/elasticsearch": "^7.6",
"psr/log": "^1.1",
"ext-json": "*"
Expand All @@ -17,7 +17,8 @@
"friendsofphp/php-cs-fixer": "^2.11",
"sebastian/phpcpd": "^6.0",
"vimeo/psalm": "^4.0",
"nette/php-generator": "^3.2"
"nette/php-generator": "^3.2",
"psalm/plugin-phpunit": "^0.15.1"
},
"autoload": {
"psr-4": {
Expand All @@ -33,9 +34,16 @@
"scripts": {
"test": "vendor/bin/phpunit --configuration phpunit.xml.dist",
"coverage": "vendor/bin/phpunit --configuration phpunit.xml.dist --coverage-html=tests/coverage",
"fixer": "vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v",
"linter": "vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run --stop-on-violation && vendor/bin/phpcpd ./ --exclude vendor --exclude tests --exclude src/Entity --exclude src/Serializer && vendor/bin/psalm --show-info=true",
"entities": "php -f generator/generate_entities.php && vendor/bin/php-cs-fixer fix --config=.php_cs.dist -q"
"fixer": "vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --allow-risky=yes",
"linter": [
"vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run --stop-on-violation --allow-risky=yes",
"vendor/bin/phpcpd ./ --exclude vendor --exclude tests --exclude src/Entity --exclude src/Serializer",
"vendor/bin/psalm --show-info=true"
],
"entities": [
"php -f generator/generate_entities.php",
"vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --allow-risky=yes -q"
]
},
"repositories": [
{
Expand Down
6 changes: 3 additions & 3 deletions generator/MapperGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private function decorateNameGetter(Method $method, EntityDescriptor $descriptor
{
$name = strtolower($this->unifyClassName($descriptor->getName()));

$method->addComment('@inheritDoc');
$method->addComment('{@inheritDoc}');
$method->setVisibility('public');
$method->setReturnType('string');
$method->setBody("return '{$name}';");
Expand All @@ -92,7 +92,7 @@ private function decorateNameGetter(Method $method, EntityDescriptor $descriptor
*/
private function decorateMapGetter(Method $method, EntityDescriptor $descriptor): void
{
$method->addComment('@inheritDoc');
$method->addComment('{@inheritDoc}');
$method->setVisibility('public');
$method->setReturnType('array');

Expand Down Expand Up @@ -120,7 +120,7 @@ private function decorateMapGetter(Method $method, EntityDescriptor $descriptor)
*/
private function decoratePrimaryNameGetter(Method $method, EntityDescriptor $descriptor): void
{
$method->addComment('@inheritDoc');
$method->addComment('{@inheritDoc}');
$method->setVisibility('public');
$method->setReturnType('string');

Expand Down
38 changes: 30 additions & 8 deletions generator/MapperTestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function generateClassByDescriptor(EntityDescriptor $descriptor, SplFi
$phpFile->setStrictTypes();

$namespace = $phpFile->addNamespace($namespace);
$this->decorateNamespace($namespace, 'Liquetsoft\\Fias\\Elastic\\IndexMapper', $baseName);
$this->decorateNamespace($descriptor, $namespace, 'Liquetsoft\\Fias\\Elastic\\IndexMapper', $baseName);

$class = $namespace->addClass($name);
$this->decorateClass($class, $descriptor);
Expand All @@ -53,12 +53,23 @@ protected function generateClassByDescriptor(EntityDescriptor $descriptor, SplFi
* @param string $baseNamespace
* @param string $baseName
*/
protected function decorateNamespace(PhpNamespace $namespace, string $baseNamespace, string $baseName): void
protected function decorateNamespace(EntityDescriptor $descriptor, PhpNamespace $namespace, string $baseNamespace, string $baseName): void
{
$hasDateTime = false;
foreach ($descriptor->getFields() as $field) {
if ($field->getSubType() === 'date') {
$hasDateTime = true;
break;
}
}

if ($hasDateTime) {
$namespace->addUse('DateTimeImmutable');
}

$namespace->addUse('Liquetsoft\\Fias\\Elastic\\Tests\\BaseCase');
$namespace->addUse('Liquetsoft\\Fias\\Elastic\\QueryBuilder\\QueryBuilder;');
$namespace->addUse('Liquetsoft\\Fias\\Elastic\\QueryBuilder\\QueryBuilder');
$namespace->addUse('stdClass');
$namespace->addUse('DateTime');
$namespace->addUse($baseNamespace . '\\' . $baseName);
}

Expand All @@ -73,7 +84,7 @@ protected function decorateClass(ClassType $class, EntityDescriptor $descriptor)
$class->setExtends('Liquetsoft\\Fias\\Elastic\\Tests\\BaseCase');
$description = trim($descriptor->getDescription(), " \t\n\r\0\x0B.");
if ($description) {
$class->addComment("Тест для описания индекса сущности '{$description}'.\n");
$class->addComment("Тест для описания индекса сущности '{$description}'.\n\n@internal");
}
}

Expand All @@ -85,6 +96,7 @@ protected function decorateClass(ClassType $class, EntityDescriptor $descriptor)
*/
private function decorateNameTest(Method $method, EntityDescriptor $descriptor): void
{
$method->setReturnType('void');
$name = $this->getTestedObjectName($descriptor);
$lowerName = strtolower($descriptor->getName());

Expand All @@ -101,12 +113,12 @@ private function decorateNameTest(Method $method, EntityDescriptor $descriptor):
*/
private function decorateMapTest(Method $method, EntityDescriptor $descriptor): void
{
$method->setReturnType('void');
$name = $this->getTestedObjectName($descriptor);

$method->addBody("\$mapper = new $name();");
$method->addBody('$map = $mapper->getMappingProperties();');
$method->addBody('');
$method->addBody('$this->assertIsArray($map);');
foreach ($descriptor->getFields() as $field) {
$name = $this->unifyColumnName($field->getName());
$method->addBody("\$this->assertArrayHasKey('{$name}', \$map);");
Expand All @@ -121,6 +133,7 @@ private function decorateMapTest(Method $method, EntityDescriptor $descriptor):
*/
private function decorateGetPrimaryNameTest(Method $method, EntityDescriptor $descriptor): void
{
$method->setReturnType('void');
$name = $this->getTestedObjectName($descriptor);

$method->addBody("\$mapper = new $name();");
Expand All @@ -143,6 +156,8 @@ private function decorateGetPrimaryNameTest(Method $method, EntityDescriptor $de
*/
private function decorateExtractPrimaryFromEntityTest(Method $method, EntityDescriptor $descriptor): void
{
$method->setReturnType('void');

$entityName = $this->getTestedObjectName($descriptor);

$primaryName = null;
Expand All @@ -169,13 +184,15 @@ private function decorateExtractPrimaryFromEntityTest(Method $method, EntityDesc
*/
private function decorateExtractDataFromEntityTest(Method $method, EntityDescriptor $descriptor): void
{
$method->setReturnType('void');

$entityName = $this->getTestedObjectName($descriptor);

$method->addBody('$entity = new stdClass();');
foreach ($descriptor->getFields() as $field) {
$fieldName = $this->unifyColumnName($field->getName());
if ($field->getSubType() === 'date') {
$method->addBody("\$entity->{$fieldName} = new DateTime();");
$method->addBody("\$entity->{$fieldName} = new DateTimeImmutable();");
} elseif ($field->getType() === 'int') {
$method->addBody("\$entity->{$fieldName} = \$this->createFakeData()->numberBetween(1, 100000);");
} else {
Expand All @@ -187,12 +204,13 @@ private function decorateExtractDataFromEntityTest(Method $method, EntityDescrip
$method->addBody("\$mapper = new $entityName();");
$method->addBody('$dataForElastic = $mapper->extractDataFromEntity($entity);');
$method->addBody('');
$method->addBody('$this->assertIsArray($dataForElastic);');
foreach ($descriptor->getFields() as $field) {
$fieldName = $this->unifyColumnName($field->getName());
$method->addBody("\$this->assertArrayHasKey('{$fieldName}', \$dataForElastic);");
if ($field->getSubType() === 'date') {
$method->addBody("\$this->assertSame(\$entity->{$fieldName}->format('Y-m-d\TH:i:s'), \$dataForElastic['{$fieldName}'], 'Test {$fieldName} field conversion.');");
} elseif ($field->getType() === 'string') {
$method->addBody("\$this->assertSame(\$entity->{$fieldName}, \$dataForElastic['{$fieldName}'], 'Test {$fieldName} field conversion.');");
} elseif ($field->isPrimary() || $field->isIndex()) {
$method->addBody("\$this->assertSame((string) \$entity->{$fieldName}, \$dataForElastic['{$fieldName}'], 'Test {$fieldName} field conversion.');");
} else {
Expand All @@ -209,6 +227,8 @@ private function decorateExtractDataFromEntityTest(Method $method, EntityDescrip
*/
private function decorateHasPropertyTest(Method $method, EntityDescriptor $descriptor): void
{
$method->setReturnType('void');

$entityName = $this->getTestedObjectName($descriptor);

$fields = $descriptor->getFields();
Expand All @@ -229,6 +249,8 @@ private function decorateHasPropertyTest(Method $method, EntityDescriptor $descr
*/
private function decorateQueryTest(Method $method, EntityDescriptor $descriptor): void
{
$method->setReturnType('void');

$entityName = $this->getTestedObjectName($descriptor);

$method->addBody("\$mapper = new $entityName();");
Expand Down
11 changes: 5 additions & 6 deletions generator/ModelTestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ protected function decorateNamespace(PhpNamespace $namespace, EntityDescriptor $
$namespace->addUse($baseNamespace . '\\' . $baseName);
foreach ($descriptor->getFields() as $field) {
if ($field->getSubType() === 'date') {
$namespace->addUse('DateTime');
$namespace->addUse('DateTimeInterface');
$namespace->addUse('DateTimeImmutable');
}
}
}
Expand All @@ -72,7 +71,7 @@ protected function decorateClass(ClassType $class, EntityDescriptor $descriptor)
$class->setExtends('Liquetsoft\\Fias\\Elastic\\Tests\\EntityCase');
$description = trim($descriptor->getDescription(), " \t\n\r\0\x0B.");
if ($description) {
$class->addComment("Тест для сущности '{$description}'.\n");
$class->addComment("Тест для сущности '{$description}'.\n\n@internal");
}
}

Expand All @@ -85,7 +84,7 @@ protected function decorateClass(ClassType $class, EntityDescriptor $descriptor)
private function decorateCreateEntity(Method $createEntityMethod, EntityDescriptor $descriptor): void
{
$baseName = $this->unifyClassName($descriptor->getName());
$createEntityMethod->addComment("@inheritdoc\n");
$createEntityMethod->addComment("{@inheritDoc}\n");
$createEntityMethod->setVisibility('protected');
$createEntityMethod->setBody("return new {$baseName}();");
}
Expand All @@ -106,7 +105,7 @@ private function decorateCreateAccessors(Method $accessorsProviderMethod, Entity
if ($type === 'int') {
$value = '$this->createFakeData()->numberBetween(1, 1000000)';
} elseif ($type === 'string_date') {
$value = 'new DateTime()';
$value = 'new DateTimeImmutable()';
}
$accessors .= " '{$name}' => {$value},\n";
}
Expand All @@ -115,6 +114,6 @@ private function decorateCreateAccessors(Method $accessorsProviderMethod, Entity
$accessorsProviderMethod->setVisibility('protected');
$accessorsProviderMethod->setReturnType('array');
$accessorsProviderMethod->setBody($accessors);
$accessorsProviderMethod->addComment("@inheritdoc\n");
$accessorsProviderMethod->addComment("{@inheritDoc}\n");
}
}
10 changes: 3 additions & 7 deletions generator/NormalizerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\PsrPrinter;
use SplFileInfo;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
Expand Down Expand Up @@ -52,7 +51,6 @@ protected function generate(SplFileInfo $dir, string $namespace): void
protected function decorateNamespace(PhpNamespace $namespace): void
{
$namespace->addUse(NormalizerInterface::class);
$namespace->addUse(InvalidArgumentException::class);
$namespace->addUse(Exception::class);

$descriptors = $this->registry->getDescriptors();
Expand Down Expand Up @@ -97,16 +95,15 @@ protected function decorateClass(ClassType $class): void
$class->addComment('Скомпилированный класс для нормализации сущностей ФИАС в модели для elasticsearch.');

$supports = $class->addMethod('supportsNormalization')
->addComment("@inheritDoc\n")
->addComment('{@inheritDoc}')
->setVisibility('public')
->setBody($supportsBody);
$supports->addParameter('data');
$supports->addParameter('format', new PhpLiteral('null'))->setType('string');

$denormalize = $class->addMethod('normalize')
->addComment("{@inheritDoc}\n")
->addComment("\n")
->addComment("@throws Exception\n")
->addComment('@throws Exception')
->setVisibility('public')
->setBody($denormalizeBody);
$denormalize->addParameter('object');
Expand Down Expand Up @@ -137,7 +134,7 @@ protected function decorateModelDataGetter(Method $method, EntityDescriptor $des
$type = trim($field->getType() . '_' . $field->getSubType(), ' _');
switch ($type) {
case 'string_date':
$varType = "'{$column}' => (\$date = \$object->{$getter}()) ? \$date->format(DATE_ATOM) : null";
$varType = "'{$column}' => (\$date = \$object->{$getter}()) ? \$date->format(\DATE_ATOM) : null";
break;
default:
$varType = "'{$column}' => \$object->{$getter}()";
Expand All @@ -149,7 +146,6 @@ protected function decorateModelDataGetter(Method $method, EntityDescriptor $des

$method->addComment("Возвращает все свойства модели '{$className}'.\n");
$method->addComment("@param {$className} \$object\n");
$method->addComment("\n");
$method->addComment('@return array');
$method->addParameter('object')->setType($this->createModelClass($descriptor));
$method->setVisibility('protected');
Expand Down
Loading

0 comments on commit bac0273

Please sign in to comment.