Skip to content

Commit

Permalink
Phpstan: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jan 8, 2022
1 parent 7adc9ee commit c1e6c5e
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 32 deletions.
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
install:
composer update

qa:
qa: phpstan cs

cs:
vendor/bin/linter src tests
vendor/bin/codesniffer src tests

phpstan:
vendor/bin/phpstan analyse -l 8 -c phpstan.neon src

tests:
vendor/bin/tester -s -p php --colors 1 -C tests/unit

coverage-clover:
vendor/bin/tester -s -p phpdbg --colors 1 -C --coverage ./coverage.xml --coverage-src ./src tests/unit

coverage-html:
vendor/bin/tester -s -p phpdbg --colors 1 -C --coverage ./coverage.html --coverage-src ./src tests/unit
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
"doctrine/inflector": "^2.0.0"
},
"require-dev": {
"ninjify/qa": "^0.13",
"nette/tester": "^2.0",
"ninjify/nunjuck": "^0.4",
"ninjify/qa": "^0.12",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-nette": "^1.0",
"phpstan/phpstan-strict-rules": "^1.0",
"tracy/tracy": "^2.3.0"
},
"autoload": {
Expand Down
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-nette/extension.neon
- vendor/phpstan/phpstan-nette/rules.neon

parameters:
checkGenericClassInNonGenericObjectType: false
6 changes: 3 additions & 3 deletions src/Analyser/Database/DatabaseAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DatabaseAnalyser implements IAnalyser
public function __construct(string $dns, string $username, ?string $password = null)
{
$this->connection = new Connection($dns, $username, $password);
$this->driver = $this->connection->getSupplementalDriver();
$this->driver = $this->connection->getDriver();
}

public function getConnection(): Connection
Expand Down Expand Up @@ -75,10 +75,10 @@ protected function analyseColumns(Table $table): void
// Analyse ENUM
if ($col['nativetype'] === ColumnTypes::NATIVE_TYPE_ENUM) {
$enum = Strings::matchAll($col['vendor']['type'] ?? $col['vendor']['Type'], ColumnTypes::NATIVE_REGEX_ENUM, PREG_PATTERN_ORDER);
if ($enum) {
if ($enum !== []) {
$column->setEnum($enum[1]);
$column->setType(ColumnTypes::TYPE_ENUM);
$column->setSubType(Helpers::columnType($col['nativetype']));
$column->setSubtype(Helpers::columnType($col['nativetype']));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Config implements ArrayAccess
public function __construct(array $configuration)
{
// Validate config
if ($extra = array_diff_key((array) $configuration, $this->defaults)) {
if ($extra = array_diff_key($configuration, $this->defaults)) {
$extra = implode(', ', array_keys($extra));
throw new InvalidStateException('Unknown configuration option ' . $extra . '.');
}
Expand Down
11 changes: 8 additions & 3 deletions src/Entity/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace Contributte\Nextras\Orm\Generator\Entity;

use Contributte\Nextras\Orm\Generator\Exception\InvalidAttachException;
use LogicException;

class Column
{

/** @var Table */
/** @var Table|null */
private $table;

/** @var string */
Expand All @@ -31,10 +32,10 @@ class Column
/** @var bool */
private $onUpdate;

/** @var PhpDoc */
/** @var PhpDoc|null */
private $phpDoc;

/** @var bool */
/** @var bool|null */
private $primary;

/** @var bool */
Expand All @@ -57,6 +58,10 @@ public function attach(Table $table): void

public function getTable(): Table
{
if (!$this->table) {
throw new LogicException('Table is needed');
}

return $this->table;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Entity/PhpDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PhpDoc
/** @var bool */
private $primary;

/** @var PhpRelDoc */
/** @var PhpRelDoc|null */
private $relation;

public function getAnnotation(): string
Expand Down Expand Up @@ -91,7 +91,7 @@ public function setVirtual(bool $virtual): void
$this->virtual = (bool) $virtual;
}

public function getRelation(): PhpRelDoc
public function getRelation(): ?PhpRelDoc
{
return $this->relation;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Entity/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use Contributte\Nextras\Orm\Generator\Exception\InvalidAttachException;
use InvalidArgumentException;
use LogicException;

class Table
{

/** @var Database */
/** @var Database|null */
private $database;

/** @var string */
Expand All @@ -28,6 +29,10 @@ public function attach(Database $database): void

public function getDatabase(): Database
{
if (!$this->database) {
throw new LogicException('Database is needed');
}

return $this->database;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Contributte\Nextras\Orm\Generator\Analyser\IAnalyser;
use Contributte\Nextras\Orm\Generator\Config\Config;
use Contributte\Nextras\Orm\Generator\Generator\IGenerator;
use Contributte\Nextras\Orm\Generator\Generator\Model\ModelGenerator;

class Generator
{
Expand All @@ -28,7 +27,7 @@ class Generator
/** @var IGenerator */
private $facadeGenerator;

/** @var ModelGenerator */
/** @var IGenerator */
private $modelGenerator;

public function __construct(Config $config, IAnalyser $analyser)
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Entity/Decorator/ColumnDocumentor.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected function getRealUse(Table $table, PhpNamespace $namespace)
return null;
}

$use = $namespace->unresolveName(
$use = $namespace->simplifyName(
$this->resolver->resolveEntityNamespace($table) . Helpers::NS . $this->resolver->resolveEntityName($table)
);

Expand Down
4 changes: 2 additions & 2 deletions src/Resolver/Impl/SimpleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Contributte\Nextras\Orm\Generator\Resolver\IModelResolver;
use Contributte\Nextras\Orm\Generator\Resolver\IRepositoryResolver;
use Contributte\Nextras\Orm\Generator\Utils\Helpers;
use Doctrine\Common\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;

abstract class SimpleResolver implements IEntityResolver, IRepositoryResolver, IMapperResolver, IFacadeResolver, IModelResolver
{
Expand Down Expand Up @@ -45,7 +45,7 @@ protected function table(Table $table, bool $singularize = false): string
$name = $this->normalize(ucfirst($table->getName()));

if ($singularize) {
$name = Inflector::singularize($name);
$name = InflectorFactory::create()->build()->singularize($name);
}

return $name;
Expand Down
10 changes: 5 additions & 5 deletions src/Resolver/Impl/SimpleSeparateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Contributte\Nextras\Orm\Generator\Entity\Table;
use Contributte\Nextras\Orm\Generator\Resolver\IFilenameResolver;
use Doctrine\Common\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;

class SimpleSeparateResolver extends SimpleResolver
{
Expand Down Expand Up @@ -75,8 +75,8 @@ public function resolveFacadeFilename(Table $table): string
protected function resolveFilenameFor(string $type, Table $table): string
{
$name = $this->normalize(ucfirst($table->getName()));
if ($this->config->get($type . '.name.singularize')) {
$name = Inflector::singularize($name);
if (!empty($this->config->get($type . '.name.singularize'))) {
$name = InflectorFactory::create()->build()->singularize($name);
}

$name .= $this->config->get($type . '.filename.suffix');
Expand All @@ -86,8 +86,8 @@ protected function resolveFilenameFor(string $type, Table $table): string
protected function resolveNameFor(string $type, Table $table): string
{
$name = $this->normalize(ucfirst($table->getName()));
if ($this->config->get($type . '.name.singularize')) {
$name = Inflector::singularize($name);
if (!empty($this->config->get($type . '.name.singularize'))) {
$name = InflectorFactory::create()->build()->singularize($name);
}

return $name . $this->config->get($type . '.name.suffix');
Expand Down
10 changes: 5 additions & 5 deletions src/Resolver/Impl/SimpleTogetherResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Contributte\Nextras\Orm\Generator\Entity\Table;
use Contributte\Nextras\Orm\Generator\Resolver\IFilenameResolver;
use Contributte\Nextras\Orm\Generator\Utils\Helpers;
use Doctrine\Common\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;

class SimpleTogetherResolver extends SimpleResolver
{
Expand Down Expand Up @@ -66,8 +66,8 @@ public function resolveFacadeName(Table $table): string
protected function resolveName(string $type, Table $table): string
{
$name = ucfirst($table->getName());
if ($this->config->get($type . '.name.singularize')) {
$name = Inflector::singularize($name);
if (!empty($this->config->get($type . '.name.singularize'))) {
$name = InflectorFactory::create()->build()->singularize($name);
}

$name .= $this->config->get($type . '.name.suffix');
Expand All @@ -78,8 +78,8 @@ protected function resolveFilenameFor(string $type, Table $table): string
{
$folder = $this->table($table, $this->config->get('orm.singularize'));
$name = ucfirst($table->getName());
if ($this->config->get($type . '.name.singularize')) {
$name = Inflector::singularize($name);
if (!empty($this->config->get($type . '.name.singularize'))) {
$name = InflectorFactory::create()->build()->singularize($name);
}

$filename = $this->normalize($name . $this->config->get($type . '.filename.suffix')) . '.' . IFilenameResolver::PHP_EXT;
Expand Down
8 changes: 4 additions & 4 deletions src/Utils/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class Helpers
public static function camelCase(string $s): string
{
$s = trim($s);
$s = preg_replace('#[^a-zA-Z0-9_-]#', ' ', $s);
$s = preg_replace('#[_-](?=[a-z])#', ' ', $s);
$s = (string) preg_replace('#[^a-zA-Z0-9_-]#', ' ', $s);
$s = (string) preg_replace('#[_-](?=[a-z])#', ' ', $s);
$s = substr(ucwords('x' . $s), 1);
$s = str_replace(' ', '', $s);
return $s;
Expand All @@ -54,7 +54,7 @@ public static function columnType(string $type): string
if (!isset($cache[$type])) {
$cache[$type] = 'string';
foreach (self::$typePatterns as $s => $val) {
if (preg_match('#' . $s . '#i', $type)) {
if (preg_match('#' . $s . '#i', $type) !== false) {
return $cache[$type] = $val;
}
}
Expand All @@ -65,7 +65,7 @@ public static function columnType(string $type): string

public static function stripMnDelimiters(string $s, ?string $r = null): string
{
return str_replace(self::$mnDelimiters, $r, $s);
return str_replace(self::$mnDelimiters, (string) $r, $s);
}

}

0 comments on commit c1e6c5e

Please sign in to comment.