Skip to content
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

0.4.3 #34

Merged
merged 6 commits into from
Jan 3, 2024
Merged
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
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
'strict_comparison' => true,
'strict_param' => true,
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'arguments', 'match', 'parameters']],
'unary_operator_spaces' => ['only_dec_inc' => true],
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false],
])
;
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "brainshaker95/php-to-ts-bundle",
"description": "Convert PHP model classes to TypeScript interfaces",
"version": "0.4.2",
"version": "0.4.3",
"keywords": [
"bundle",
"symfony",
Expand All @@ -16,13 +16,13 @@
}
],
"require-dev": {
"friendsofphp/php-cs-fixer": "3.35.1",
"phpstan/phpstan": "1.10.38",
"phpstan/phpstan-strict-rules": "1.5.1",
"symplify/phpstan-rules": "12.3.1",
"phpunit/phpunit": "10.4.1",
"symfony/framework-bundle": "6.3.5",
"symfony/yaml": "6.3.3",
"friendsofphp/php-cs-fixer": "3.45.0",
"phpstan/phpstan": "1.10.50",
"phpstan/phpstan-strict-rules": "1.5.2",
"symplify/phpstan-rules": "12.4.3",
"phpunit/phpunit": "10.5.5",
"symfony/framework-bundle": "6.4.2",
"symfony/yaml": "6.4.0",
"cweagans/composer-patches": "1.7.3"
},
"require": {
Expand Down
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ rules:
- Symplify\PHPStanRules\Rules\CheckClassNamespaceFollowPsr4Rule
- Symplify\PHPStanRules\Rules\CheckTypehintCallerTypeRule
- Symplify\PHPStanRules\Rules\Complexity\ForbiddenSameNamedNewInstanceRule
- Symplify\PHPStanRules\Rules\Complexity\NoDuplicatedTraitMethodNameRule
- Symplify\PHPStanRules\Rules\Domain\RequireAttributeNamespaceRule
- Symplify\PHPStanRules\Rules\Domain\RequireExceptionNamespaceRule
- Symplify\PHPStanRules\Rules\Enum\RequireUniqueEnumConstantRule
Expand All @@ -31,17 +30,14 @@ rules:
- Symplify\PHPStanRules\Rules\NarrowType\NarrowPublicClassMethodParamTypeByCallerTypeRule
- Symplify\PHPStanRules\Rules\NoAbstractMethodRule
- Symplify\PHPStanRules\Rules\NoArrayAccessOnObjectRule
- Symplify\PHPStanRules\Rules\NoConstructorInTestRule
- Symplify\PHPStanRules\Rules\NoDynamicNameRule
- Symplify\PHPStanRules\Rules\NoEmptyClassRule
- Symplify\PHPStanRules\Rules\NoInlineStringRegexRule
- Symplify\PHPStanRules\Rules\NoIssetOnObjectRule
- Symplify\PHPStanRules\Rules\NoMissingDirPathRule
- Symplify\PHPStanRules\Rules\NoNullableArrayPropertyRule
- Symplify\PHPStanRules\Rules\NoParentMethodCallOnNoOverrideProcessRule
- Symplify\PHPStanRules\Rules\NoReferenceRule
- Symplify\PHPStanRules\Rules\NoVoidGetterMethodRule
- Symplify\PHPStanRules\Rules\PHPUnit\NoRightPHPUnitAssertScalarRule
- Symplify\PHPStanRules\Rules\PreventParentMethodVisibilityOverrideRule
- Symplify\PHPStanRules\Rules\RegexSuffixInRegexConstantRule
- Symplify\PHPStanRules\Rules\RequireAttributeNameRule
Expand Down
4 changes: 2 additions & 2 deletions src/Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ abstract class DumpCommand extends Command
use HasConfiguration;
use HasDumper;

final public const INDENT_STYLE_KEY = C::INDENT_KEY . '-' . C::INDENT_STYLE_KEY;
final public const INDENT_COUNT_KEY = C::INDENT_KEY . '-' . C::INDENT_COUNT_KEY;
final protected const INDENT_STYLE_KEY = C::INDENT_KEY . '-' . C::INDENT_STYLE_KEY;
final protected const INDENT_COUNT_KEY = C::INDENT_KEY . '-' . C::INDENT_COUNT_KEY;

protected InputInterface $input;

Expand Down
78 changes: 39 additions & 39 deletions src/Interface/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,50 @@
*/
interface Config
{
public const OUTPUT_DIR_KEY = 'output_dir';
public const OUTPUT_DIR_DEFAULT = 'assets/ts/types/php-to-ts';
public const OUTPUT_DIR_DESC = 'Directory in which to dump TypeScript interfaces';

public const INPUT_DIR_KEY = 'input_dir';
public const INPUT_DIR_DEFAULT = 'src/Model';
public const INPUT_DIR_DESC = 'Directory in which to look for models to include';

public const FILE_TYPE_KEY = 'file_type';
public const FILE_TYPE_DEFAULT = FileType::TYPE_MODULE;
public const FILE_TYPE_DESC = 'File type to use for TypeScript interfaces';
public const FILE_TYPE_VALID_VALUES = [FileType::TYPE_DECLARATION, FileType::TYPE_MODULE];

public const TYPE_DEFINITION_TYPE_KEY = 'type_definition_type';
public const TYPE_DEFINITION_TYPE_DEFAULT = TypeDefinitionType::TYPE_INTERFACE;
public const TYPE_DEFINITION_TYPE_DESC = 'Type definition type to use for TypeScript interfaces';
public const TYPE_DEFINITION_TYPE_VALID_VALUES = [TypeDefinitionType::TYPE_INTERFACE, TypeDefinitionType::TYPE_TYPE_ALIAS];

public const INDENT_KEY = 'indent';
public const INDENT_DESC = 'Indentation used for TypeScript interfaces';
public const INDENT_STYLE_KEY = 'style';
public const INDENT_STYLE_DEFAULT = Indent::STYLE_SPACE;
public const INDENT_STYLE_DESC = 'Indent style used for TypeScript interfaces';
public const INDENT_STYLE_VALID_VALUES = [Indent::STYLE_SPACE, Indent::STYLE_TAB];
public const INDENT_COUNT_KEY = 'count';
public const INDENT_COUNT_DEFAULT = 2;
public const INDENT_COUNT_DESC = 'Number of indent style characters per indent';

public const QUOTES_KEY = 'quotes';
public const QUOTES_DESC = 'Quote style used for strings in TypeScript interfaces';
public const QUOTES_DEFAULT = Quotes::STYLE_SINGLE;
public const QUOTES_VALID_VALUES = [Quotes::STYLE_DOUBLE, Quotes::STYLE_SINGLE];

public const SORT_STRATEGIES_KEY = 'sort_strategies';
public const SORT_STRATEGIES_DEFAULT = [
final public const OUTPUT_DIR_KEY = 'output_dir';
final public const OUTPUT_DIR_DEFAULT = 'assets/ts/types/php-to-ts';
final public const OUTPUT_DIR_DESC = 'Directory in which to dump TypeScript interfaces';

final public const INPUT_DIR_KEY = 'input_dir';
final public const INPUT_DIR_DEFAULT = 'src/Model';
final public const INPUT_DIR_DESC = 'Directory in which to look for models to include';

final public const FILE_TYPE_KEY = 'file_type';
final public const FILE_TYPE_DEFAULT = FileType::TYPE_MODULE;
final public const FILE_TYPE_DESC = 'File type to use for TypeScript interfaces';
final public const FILE_TYPE_VALID_VALUES = [FileType::TYPE_DECLARATION, FileType::TYPE_MODULE];

final public const TYPE_DEFINITION_TYPE_KEY = 'type_definition_type';
final public const TYPE_DEFINITION_TYPE_DEFAULT = TypeDefinitionType::TYPE_INTERFACE;
final public const TYPE_DEFINITION_TYPE_DESC = 'Type definition type to use for TypeScript interfaces';
final public const TYPE_DEFINITION_TYPE_VALID_VALUES = [TypeDefinitionType::TYPE_INTERFACE, TypeDefinitionType::TYPE_TYPE_ALIAS];

final public const INDENT_KEY = 'indent';
final public const INDENT_DESC = 'Indentation used for TypeScript interfaces';
final public const INDENT_STYLE_KEY = 'style';
final public const INDENT_STYLE_DEFAULT = Indent::STYLE_SPACE;
final public const INDENT_STYLE_DESC = 'Indent style used for TypeScript interfaces';
final public const INDENT_STYLE_VALID_VALUES = [Indent::STYLE_SPACE, Indent::STYLE_TAB];
final public const INDENT_COUNT_KEY = 'count';
final public const INDENT_COUNT_DEFAULT = 2;
final public const INDENT_COUNT_DESC = 'Number of indent style characters per indent';

final public const QUOTES_KEY = 'quotes';
final public const QUOTES_DESC = 'Quote style used for strings in TypeScript interfaces';
final public const QUOTES_DEFAULT = Quotes::STYLE_SINGLE;
final public const QUOTES_VALID_VALUES = [Quotes::STYLE_DOUBLE, Quotes::STYLE_SINGLE];

final public const SORT_STRATEGIES_KEY = 'sort_strategies';
final public const SORT_STRATEGIES_DEFAULT = [
AlphabeticalAsc::class,
ConstructorFirst::class,
ReadonlyFirst::class,
];
public const SORT_STRATEGIES_DESC = 'Class names of sort strategies used for TypeScript properties';
final public const SORT_STRATEGIES_DESC = 'Class names of sort strategies used for TypeScript properties';

public const FILE_NAME_STRATEGY_KEY = 'file_name_strategy';
public const FILE_NAME_STRATEGY_DEFAULT = KebabCase::class;
public const FILE_NAME_STRATEGY_DESC = 'Class name of file name strategies used for TypeScript files';
final public const FILE_NAME_STRATEGY_KEY = 'file_name_strategy';
final public const FILE_NAME_STRATEGY_DEFAULT = KebabCase::class;
final public const FILE_NAME_STRATEGY_DESC = 'Class name of file name strategies used for TypeScript files';

public function getInputDir(): ?string;

Expand Down
3 changes: 2 additions & 1 deletion src/Model/Ast/ConstExpr/ConstExprFalseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Brainshaker95\PhpToTsBundle\Model\Ast\ConstExpr;

use Brainshaker95\PhpToTsBundle\Interface\Node;
use Brainshaker95\PhpToTsBundle\Model\TsProperty;
use Brainshaker95\PhpToTsBundle\Tool\Assert;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFalseNode as PHPStanConstExprFalseNode;
use PHPStan\PhpDocParser\Ast\Node as PHPStanNode;
Expand All @@ -21,7 +22,7 @@ public function __toString(): string

public function toString(): string
{
return 'false';
return TsProperty::TYPE_FALSE;
}

public static function fromPhpStan(PHPStanNode $node): self
Expand Down
3 changes: 2 additions & 1 deletion src/Model/Ast/ConstExpr/ConstExprNullNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Brainshaker95\PhpToTsBundle\Model\Ast\ConstExpr;

use Brainshaker95\PhpToTsBundle\Interface\Node;
use Brainshaker95\PhpToTsBundle\Model\TsProperty;
use Brainshaker95\PhpToTsBundle\Tool\Assert;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNullNode as PHPStanConstExprNullNode;
use PHPStan\PhpDocParser\Ast\Node as PHPStanNode;
Expand All @@ -21,7 +22,7 @@ public function __toString(): string

public function toString(): string
{
return 'null';
return TsProperty::TYPE_NULL;
}

public static function fromPhpStan(PHPStanNode $node): self
Expand Down
3 changes: 2 additions & 1 deletion src/Model/Ast/ConstExpr/ConstExprTrueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Brainshaker95\PhpToTsBundle\Model\Ast\ConstExpr;

use Brainshaker95\PhpToTsBundle\Interface\Node;
use Brainshaker95\PhpToTsBundle\Model\TsProperty;
use Brainshaker95\PhpToTsBundle\Tool\Assert;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode as PHPStanConstExprTrueNode;
use PHPStan\PhpDocParser\Ast\Node as PHPStanNode;
Expand All @@ -21,7 +22,7 @@ public function __toString(): string

public function toString(): string
{
return 'true';
return TsProperty::TYPE_TRUE;
}

public static function fromPhpStan(PHPStanNode $node): self
Expand Down
3 changes: 2 additions & 1 deletion src/Model/Ast/Type/NullableTypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Brainshaker95\PhpToTsBundle\Model\Ast\Type;

use Brainshaker95\PhpToTsBundle\Interface\Node;
use Brainshaker95\PhpToTsBundle\Model\TsProperty;
use Brainshaker95\PhpToTsBundle\Tool\Assert;
use Brainshaker95\PhpToTsBundle\Tool\PhpStan;
use PHPStan\PhpDocParser\Ast\Node as PHPStanNode;
Expand All @@ -26,7 +27,7 @@ public function __toString(): string

public function toString(): string
{
return '(' . $this->type . ' | null)';
return '(' . $this->type . ' | ' . TsProperty::TYPE_NULL . ')';
}

public static function fromPhpStan(PHPStanNode $node): self
Expand Down
4 changes: 3 additions & 1 deletion src/Model/Config/FileType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Brainshaker95\PhpToTsBundle\Model\Config;

abstract class FileType
final class FileType
{
public const TYPE_DECLARATION = 'declaration';
public const TYPE_MODULE = 'module';

private function __construct() {}
}
4 changes: 3 additions & 1 deletion src/Model/Config/TypeDefinitionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Brainshaker95\PhpToTsBundle\Model\Config;

abstract class TypeDefinitionType
final class TypeDefinitionType
{
public const TYPE_INTERFACE = 'interface';
public const TYPE_TYPE_ALIAS = 'type';

private function __construct() {}
}
34 changes: 25 additions & 9 deletions src/Serializer/Normalizer/EnumNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Brainshaker95\PhpToTsBundle\Serializer\Normalizer;

use ArrayObject;
use BackedEnum;
use Brainshaker95\PhpToTsBundle\Attribute\AsTypeScriptable;
use Brainshaker95\PhpToTsBundle\Tool\Attribute;
Expand All @@ -17,33 +18,48 @@ final class EnumNormalizer implements NormalizerInterface
{
/**
* @param mixed[] $context
*
* @return array<mixed>|string|int|float|bool|ArrayObject<int|string,mixed>|null
*/
public function normalize(
mixed $enum,
mixed $data,
?string $format = null,
array $context = [],
): int|string {
if (!is_object($enum)) {
): array|string|int|float|bool|ArrayObject|null {
if (!is_object($data)) {
throw new InvalidArgumentException(sprintf(
'Expected paramteter 1 ($enum) to be of type "object" but got "%s".',
get_debug_type($enum),
'Expected paramteter 1 ($data) to be of type "object" but got "%s".',
get_debug_type($data),
));
}

if (!$enum instanceof BackedEnum) {
if (!$data instanceof BackedEnum) {
throw new InvalidArgumentException(sprintf(
'Expected object to be an instance of "%s". Given instance was of class "%s".',
BackedEnum::class,
$enum::class,
$data::class,
));
}

return $enum->value;
return $data->value;
}

public function supportsNormalization($data, ?string $format = null): bool
/**
* @param mixed[] $context
*/
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof BackedEnum
&& Attribute::existsOnClass(AsTypeScriptable::class, $data);
}

/**
* @return array<class-string|'*'|'object'|string,bool|null>
*/
public function getSupportedTypes(?string $format): array
{
return [
BackedEnum::class => true,
];
}
}
2 changes: 1 addition & 1 deletion src/Service/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private static function getFqcn(ClassLike $node): ?string
* @var ?class-string
*/
return $node->namespacedName
? implode('\\', $node->namespacedName->parts)
? implode('\\', $node->namespacedName->getParts())
: $node->name?->name;
}
}
Loading