Skip to content
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
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

[*.neon]
indent_style = tab

[*.neon.dist]
indent_style = tab

[*.yml]
indent_size = 2

Expand Down
2 changes: 1 addition & 1 deletion .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpstan" version="2.1.33" installed="2.1.33" location="./tools/phpstan" copy="false"/>
<phar name="phpstan" version="2.1.39" installed="2.1.39" location="./tools/phpstan" copy="false"/>
</phive>
16 changes: 7 additions & 9 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ includes:
- phpstan-baseline.neon

parameters:
level: 6
paths:
- src/
bootstrapFiles:
- tests/bootstrap.php
ignoreErrors:
- identifier: missingType.iterableValue
- identifier: missingType.generics
- identifier: method.childReturnType
level: 8
paths:
- src/
bootstrapFiles:
- tests/bootstrap.php
ignoreErrors:
- identifier: missingType.iterableValue
7 changes: 3 additions & 4 deletions src/BakePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Cake\Http\BaseApplication;
use DirectoryIterator;
use ReflectionClass;
use ReflectionException;

/**
* Plugin class for bake
Expand Down Expand Up @@ -140,11 +139,11 @@ protected function findInPath(string $namespace, string $path): array
$class = $namespace . $item->getBasename('.php');

if (!$hasSubfolder) {
try {
$reflection = new ReflectionClass($class);
} catch (ReflectionException) {
if (!class_exists($class)) {
continue;
}

$reflection = new ReflectionClass($class);
if (!$reflection->isInstantiable() || !$reflection->isSubclassOf(BakeCommand::class)) {
continue;
}
Expand Down
18 changes: 15 additions & 3 deletions src/CodeGen/CodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public function parseFile(string $code): ?ParsedFile
{
$this->fileText = $code;
try {
$this->traverser->traverse($this->parser->parse($code));
$ast = $this->parser->parse($code);
if ($ast === null) {
return null;
}
$this->traverser->traverse($ast);
} catch (Error $e) {
throw new ParseException($e->getMessage(), null, $e);
}
Expand Down Expand Up @@ -172,7 +176,11 @@ public function enterNode(Node $node)
throw new ParseException('Multiple constants per line are not supported, update your file');
}

$name = (string)current($constant->consts)->name;
$const = current($constant->consts);
if ($const === false) {
continue;
}
$name = (string)$const->name;
$constants[$name] = $this->getNodeCode($constant);
}

Expand All @@ -182,7 +190,11 @@ public function enterNode(Node $node)
throw new ParseException('Multiple properties per line are not supported, update your file');
}

$name = (string)current($property->props)->name;
$prop = current($property->props);
if ($prop === false) {
continue;
}
$name = (string)$prop->name;
$properties[$name] = $this->getNodeCode($property);
}

Expand Down
18 changes: 15 additions & 3 deletions src/CodeGen/ColumnTypeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public function extract(string $code): array
// Wrap code in a dummy class if needed for parsing
$wrappedCode = "<?php\nclass Dummy {\n" . $code . "\n}";
$ast = $this->parser->parse($wrappedCode);
if ($ast === null) {
return [];
}

$traverser = new NodeTraverser();
$traverser->addVisitor($this);
Expand Down Expand Up @@ -144,8 +147,13 @@ protected function processMethodCall(MethodCall $methodCall): void
) {
// Extract the column name and type expression
if (count($methodCall->args) >= 2) {
$columnArg = $methodCall->args[0]->value;
$typeArg = $methodCall->args[1]->value;
$columnArgNode = $methodCall->args[0];
$typeArgNode = $methodCall->args[1];
if (!$columnArgNode instanceof Node\Arg || !$typeArgNode instanceof Node\Arg) {
return;
}
$columnArg = $columnArgNode->value;
$typeArg = $typeArgNode->value;

// Get column name
$columnName = $this->getStringValue($columnArg);
Expand Down Expand Up @@ -199,7 +207,11 @@ protected function getTypeExpression(Node $node): ?string
if ($className === 'EnumType' || str_ends_with($className, '\\EnumType')) {
if ($methodName === 'from' && count($node->args) > 0) {
// Extract the enum class name
$arg = $node->args[0]->value;
$argNode = $node->args[0];
if (!$argNode instanceof Node\Arg) {
return null;
}
$arg = $argNode->value;
if ($arg instanceof Node\Expr\ClassConstFetch) {
if (
$arg->class instanceof Node\Name &&
Expand Down
8 changes: 7 additions & 1 deletion src/Command/BakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ protected function _getName(string $name): string
*/
protected function getPrefix(Arguments $args): string
{
/** @var string|null $prefix */
$prefix = $args->getOption('prefix');
if (!$prefix) {
return '';
Expand Down Expand Up @@ -225,7 +226,12 @@ protected function isValidColumnName(string $name): bool
protected function parseFile(string $path): ?ParsedFile
{
if (file_exists($path)) {
return (new CodeParser())->parseFile(file_get_contents($path));
$contents = file_get_contents($path);
if ($contents === false) {
return null;
}

return (new CodeParser())->parseFile($contents);
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Command/CommandCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function templateData(Arguments $arguments): array
$data['command_name'] = Inflector::underscore(str_replace(
'.',
' ',
$arguments->getArgument('name'),
$arguments->getArgument('name') ?? '',
));

return $data;
Expand Down
6 changes: 3 additions & 3 deletions src/Command/ControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function bake(string $controllerName, Arguments $args, ConsoleIo $io): vo
$actions = ['index', 'view', 'add', 'edit', 'delete'];
}
if ($args->getOption('actions')) {
$actions = array_map('trim', explode(',', $args->getOption('actions')));
$actions = array_map('trim', explode(',', (string)$args->getOption('actions')));
$actions = array_filter($actions);
}
if (!$args->getOption('actions') && Plugin::isLoaded('Authentication') && $controllerName === 'Users') {
Expand Down Expand Up @@ -221,7 +221,7 @@ public function getComponents(Arguments $args): array
{
$components = [];
if ($args->getOption('components')) {
$components = explode(',', $args->getOption('components'));
$components = explode(',', (string)$args->getOption('components'));
$components = array_values(array_filter(array_map('trim', $components)));
} else {
if (Plugin::isLoaded('Authorization')) {
Expand All @@ -242,7 +242,7 @@ public function getHelpers(Arguments $args): array
{
$helpers = [];
if ($args->getOption('helpers')) {
$helpers = explode(',', $args->getOption('helpers'));
$helpers = explode(',', (string)$args->getOption('helpers'));
$helpers = array_values(array_filter(array_map('trim', $helpers)));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Command/FixtureAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
$this->extractCommonProperties($args);

/** @var \Cake\Database\Connection $connection */
$connection = ConnectionManager::get($args->getOption('connection') ?? 'default');
$connection = ConnectionManager::get((string)($args->getOption('connection') ?: 'default'));
$scanner = new TableScanner($connection);
$fixture = new FixtureCommand();

Expand Down
6 changes: 5 additions & 1 deletion src/Command/FixtureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
return static::CODE_SUCCESS;
}

$table = $args->getOption('table') ?? '';
$table = (string)$args->getOption('table');
$model = $this->_camelize($name);
$this->bake($model, $table, $args, $io);

Expand Down Expand Up @@ -285,16 +285,19 @@ protected function _generateSchema(TableSchemaInterface $table): string
{
$cols = $indexes = $constraints = [];
foreach ($table->columns() as $field) {
/** @var array $fieldData */
$fieldData = $table->getColumn($field);
$properties = implode(', ', $this->_values($fieldData));
$cols[] = " '$field' => [$properties],";
}
foreach ($table->indexes() as $index) {
/** @var array $fieldData */
$fieldData = $table->getIndex($index);
$properties = implode(', ', $this->_values($fieldData));
$indexes[] = " '$index' => [$properties],";
}
foreach ($table->constraints() as $index) {
/** @var array $fieldData */
$fieldData = $table->getConstraint($index);
$properties = implode(', ', $this->_values($fieldData));
$constraints[] = " '$index' => [$properties],";
Expand Down Expand Up @@ -360,6 +363,7 @@ protected function _generateRecords(TableSchemaInterface $table, int $recordCoun
for ($i = 0; $i < $recordCount; $i++) {
$record = [];
foreach ($table->columns() as $field) {
/** @var array $fieldInfo */
$fieldInfo = $table->getColumn($field);
$insert = '';
switch ($fieldInfo['type']) {
Expand Down
19 changes: 13 additions & 6 deletions src/Command/ModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ public function findBelongsTo(Table $model, array $associations, ?Arguments $arg
if ($className && $className !== $tmpModelName) {
$assoc['className'] = $className;
}
if ($schema->getColumn($fieldName)['null'] === false) {
$columnInfo = $schema->getColumn($fieldName);
if ($columnInfo !== null && ($columnInfo['null'] ?? true) === false) {
$assoc['joinType'] = 'INNER';
}
}
Expand Down Expand Up @@ -484,8 +485,9 @@ public function hasUniqueConstraintFor(TableSchemaInterface $schema, string $key
foreach ($schema->constraints() as $constraint) {
$constraintInfo = $schema->getConstraint($constraint);
if (
$constraintInfo['type'] === TableSchema::CONSTRAINT_UNIQUE &&
$constraintInfo['columns'] === [$keyField]
$constraintInfo !== null &&
($constraintInfo['type'] ?? null) === TableSchema::CONSTRAINT_UNIQUE &&
($constraintInfo['columns'] ?? []) === [$keyField]
) {
return true;
}
Expand Down Expand Up @@ -663,7 +665,7 @@ public function getDisplayField(Table $model, Arguments $args): array|string
public function getPrimaryKey(Table $model, Arguments $args): array
{
if ($args->getOption('primary-key')) {
$fields = explode(',', $args->getOption('primary-key'));
$fields = explode(',', (string)$args->getOption('primary-key'));

return array_values(array_filter(array_map('trim', $fields)));
}
Expand Down Expand Up @@ -702,6 +704,7 @@ public function getEntityPropertySchema(Table $model): array

$schema = $model->getSchema();
foreach ($schema->columns() as $column) {
/** @var array $columnSchema */
$columnSchema = $schema->getColumn($column);

$properties[$column] = [
Expand Down Expand Up @@ -757,7 +760,7 @@ public function getFields(Table $table, Arguments $args): array|false|null
return false;
}
if ($args->getOption('fields')) {
$fields = explode(',', $args->getOption('fields'));
$fields = explode(',', (string)$args->getOption('fields'));

return array_values(array_filter(array_map('trim', $fields)));
}
Expand Down Expand Up @@ -786,7 +789,7 @@ public function getHiddenFields(Table $model, Arguments $args): array
return [];
}
if ($args->getOption('hidden')) {
$fields = explode(',', $args->getOption('hidden'));
$fields = explode(',', (string)$args->getOption('hidden'));

return array_values(array_filter(array_map('trim', $fields)));
}
Expand Down Expand Up @@ -932,6 +935,7 @@ public function fieldValidation(
}

foreach ($schema->constraints() as $constraint) {
/** @var array $constraint */
$constraint = $schema->getConstraint($constraint);
if (!in_array($fieldName, $constraint['columns'] ?? [], true) || count($constraint['columns']) > 1) {
continue;
Expand Down Expand Up @@ -1010,6 +1014,7 @@ public function getRules(Table $model, array $associations, Arguments $args): ar
$uniqueConstraintsColumns = [];

foreach ($schema->constraints() as $name) {
/** @var array $constraint */
$constraint = $schema->getConstraint($name);
if ($constraint['type'] !== TableSchema::CONSTRAINT_UNIQUE) {
continue;
Expand Down Expand Up @@ -1476,6 +1481,7 @@ protected function possibleEnumFields(TableSchemaInterface $schema): array
$fields = [];

foreach ($schema->columns() as $column) {
/** @var array $columnSchema */
$columnSchema = $schema->getColumn($column);
if (str_starts_with($columnSchema['type'], 'enum-')) {
$fields[] = $column;
Expand All @@ -1502,6 +1508,7 @@ protected function getEnumDefinitions(TableSchemaInterface $schema): array
$enums = [];

foreach ($schema->columns() as $column) {
/** @var array $columnSchema */
$columnSchema = $schema->getColumn($column);
if (
!in_array($columnSchema['type'], ['string', 'integer', 'tinyinteger', 'smallinteger'], true)
Expand Down
Loading