Skip to content

Commit

Permalink
move ninjify/qa to dev dependencies and update coding standards (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
dakorpar authored Sep 5, 2019
1 parent 58067ed commit a29915c
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 7 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
"php": "^7.1",
"nette/utils": "^2.4|^3.0",
"nette/php-generator": "^2.6|^3.0",
"ninjify/qa": "^0.8",
"symfony/console": "^4.2",
"doctrine/inflector": "^1.3"
},
"require-dev": {
"ninjify/qa": ">=0.8",
"phpunit/phpunit": ">=7.0"
},
"scripts": {
Expand Down Expand Up @@ -57,4 +57,4 @@
"temp/phpstan/vendor/bin/phpstan analyse -l 2 -c phpstan.neon src --memory-limit 1024M"
]
}
}
}
3 changes: 2 additions & 1 deletion src/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Entity implements ArrayAccess, IteratorAggregate, Countable
public function __construct(array $arr = [])
{
$this->data = $arr;

foreach ($arr as $k => $v) {
$this->$k = $v;
}
Expand All @@ -36,7 +37,7 @@ public function _getModifications(): array

public function tableName(): ?string
{
return static::TABLE;
return self::TABLE;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Factory/GeneratorPdoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function create(Config $config): Generator
{
$repository = new PdoRepository($this->pdo);
$generator = new Generator($repository, $config);

return $generator;
}

Expand Down
1 change: 1 addition & 0 deletions src/Generator/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function __construct(?array $config = null)
if ($config === null) {
return;
}

foreach ($config as $key => $value) {
$this->$key = $value;
}
Expand Down
23 changes: 23 additions & 0 deletions src/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,22 @@ public function generate(?string $table = null, ?string $query = null): void
if ($table === null) {
throw new Exception('When using query table argument has to be provided!');
}

$this->repository->createViewFromQuery($table, $query);
$this->generateEntity($table);
$this->repository->dropView($table);

return;
}

if ($table !== null) {
$this->generateEntity($table);

return;
}

$tables = $this->repository->getTables();

foreach ($tables as $oneTable) {
$this->generateEntity($oneTable);
}
Expand All @@ -62,31 +68,39 @@ public function generateEntity(string $table): void
$entity = $namespace->addClass($shortClassName);

$phpDocProperties = [];

if (!$this->config->rewrite && class_exists($fqnClassName)) {
$this->cloneEntityFromExistingEntity($entity, ClassType::from($fqnClassName));
$phpDocProperties = Helper::getPhpDocComments($entity->getComment() ?? '');
}

$entity->addConstant($this->config->tableConstant, $table)->setVisibility('public');
$entity->setExtends($this->config->extends);

$columns = $this->repository->getTableColumns($table);
$mapping = [];

foreach ($columns as $column) {
$this->validateColumnName($table, $column);
$this->generateColumnConstant($entity, $column);

if (isset($entity->properties[$column->getField()]) || in_array($column->getField(), $phpDocProperties, true)) {
continue;
}

$mapping[$column->getField()] = Inflector::classify($column->getField());
$this->generateColumn($entity, $column);
}

if ($this->config->generateMapping) {
if (isset($entity->properties['mapping'])) {
$mapping += $entity->getProperty('mapping')->getValue();
}

$entity->addProperty('mapping', $mapping)->setVisibility('protected')
->addComment('')->addComment('@var string[]')->addComment('');
}

file_put_contents($this->config->path . '/' . $shortClassName . '.php', $file->__toString());
}

Expand Down Expand Up @@ -143,13 +157,17 @@ protected function generateColumn(ClassType $entity, Column $column): void
protected function getColumnType(Column $column): string
{
$dbColumnType = $column->getType();

if (Strings::contains($dbColumnType, '(')) {
$dbColumnType = Strings::lower(Strings::before($dbColumnType, '('));
}

$typeMapping = Helper::multiArrayFlip($this->config->typeMapping);

if (isset($typeMapping[$dbColumnType])) {
return $typeMapping[$dbColumnType];
}

return 'string';
}

Expand All @@ -159,13 +177,16 @@ protected function generateColumnConstant(ClassType $entity, Column $column): vo
$entity->addConstant($this->config->primaryKeyConstant, $column->getField())
->setVisibility('public');
}

if ($this->config->generateColumnConstant) {
$columnConstant = $this->config->prefix . Strings::upper(Inflector::tableize($column->getField()));

if ($columnConstant === 'CLASS') {
$columnConstant = '_CLASS';
}

$constants = $entity->getConstants();

if (!isset($constants[$column->getField()])) {
$entity->addConstant($columnConstant, $column->getField())->setVisibility('public');
}
Expand Down Expand Up @@ -195,9 +216,11 @@ private function getMethodBody(string $class, string $name): string
$source = file($func->getFileName());
$bodyLines = array_slice($source, $startLine, $length);
$body = '';

foreach ($bodyLines as $bodyLine) {
$body .= Strings::after($bodyLine, "\t\t");
}

return $body;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Generator/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ class Helper
public static function multiArrayFlip(array $array): array
{
$result = [];

foreach ($array as $key => $insideArray) {
foreach ($insideArray as $value) {
$result[$value] = $key;
}
}

return $result;
}

public static function camelize(string $input, array $replacements = [], string $separator = '_'): string
{
$words = explode($separator, $input);
$result = '';

foreach ($words as $word) {
$result .= $replacements[$word] ?? Inflector::singularize(ucfirst($word));
}

return $result;
}

Expand All @@ -44,9 +48,11 @@ public static function getPhpDocComments(string $phpDoc): array
PREG_SET_ORDER
);
$result = [];

foreach ($matches as $match) {
$result[] = $match[2];
}

return $result;
}

Expand Down
1 change: 1 addition & 0 deletions src/Repository/PdoRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function getTableColumns(string $table): array
{
$query = $this->db->query('SHOW COLUMNS FROM `' . $table . '`');
$query->setFetchMode(PDO::FETCH_CLASS, Column::class);

return $query->fetchAll();
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Generator/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ public function testGenerateEntity_WithGenerateConstant_ShouldGenerateConstants(
include $entityFile;

$entityContents = file_get_contents($entityFile);
$this->assertContains('const TABLE_NAME = \'constants\'', $entityContents);
$this->assertContains('const PK_CONSTANT = \'id\'', $entityContents);
$this->assertContains('const ID = \'id\'', $entityContents);
$this->assertStringContainsString('const TABLE_NAME = \'constants\'', $entityContents);
$this->assertStringContainsString('const PK_CONSTANT = \'id\'', $entityContents);
$this->assertStringContainsString('const ID = \'id\'', $entityContents);
unlink($entityFile);
}

Expand Down
6 changes: 5 additions & 1 deletion tests/TestEntities/PhpDocPropertyEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @property int $id
* @property string $title
* @property int $published
* @property bool $published
* @property \DateTimeInterface $created_at
*/
class PhpDocPropertyEntity extends Entity
Expand All @@ -24,6 +24,7 @@ public function getId(): int
public function setId(int $value): self
{
$this['id'] = $value;

return $this;
}

Expand All @@ -37,6 +38,7 @@ public function getTitle(): ?string
public function setTitle(?string $value): self
{
$this['title'] = $value;

return $this;
}

Expand All @@ -50,6 +52,7 @@ public function getPublished(): bool
public function setPublished(bool $value): self
{
$this['published'] = $value;

return $this;
}

Expand All @@ -63,6 +66,7 @@ public function getCreatedAt(): ?\DateTimeInterface
public function setCreatedAt(?\DateTimeInterface $value): self
{
$this['created_at'] = $value;

return $this;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/TestEntities/UserEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function getId(): int
public function setId(int $value): self
{
$this['id'] = $value;

return $this;
}

Expand All @@ -43,6 +44,7 @@ public function getUsername(): string
public function setUsername(string $value): self
{
$this['username'] = $value;

return $this;
}

Expand All @@ -56,6 +58,7 @@ public function getLastLogin(): ?DateTime
public function setLastLogin(DateTime $value): self
{
$this['last_login'] = $value;

return $this;
}

Expand All @@ -69,6 +72,7 @@ public function isActive(): bool
public function setActive(bool $value): self
{
$this['active'] = $value;

return $this;
}

Expand Down

0 comments on commit a29915c

Please sign in to comment.