Skip to content

Commit

Permalink
Merge pull request #459 from cakephp/psalm
Browse files Browse the repository at this point in the history
Static analysis
  • Loading branch information
dereuromark authored May 10, 2020
2 parents 5275147 + 78643e4 commit e47728b
Show file tree
Hide file tree
Showing 31 changed files with 198 additions and 60 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
],
"cs-check": "phpcs --colors -p -s --ignore=/tests/comparisons/,/test_app/config/,/TestBlog/config/ src/ tests/",
"cs-fix": "phpcbf --colors -p --ignore=/tests/comparisons/,/test_app/config/,/TestBlog/config/ src/ tests/",
"stan": "phpstan analyse src/",
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.12 && mv composer.backup composer.json",
"stan": "phpstan analyse src/ && psalm.phar --show-info=false",
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.12 psalm/phar:~3.11.2 && mv composer.backup composer.json",
"test": "phpunit",
"test-coverage": "phpunit --coverage-clover=clover.xml"
}
Expand Down
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
parameters:
level: 5
level: 6
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
autoload_files:
- tests/bootstrap.php
36 changes: 36 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="3.11.2@d470903722cfcbc1cd04744c5491d3e6d13ec3d9">
<file src="src/CakeManager.php">
<ImplementedReturnTypeMismatch occurrences="1">
<code>array|string</code>
</ImplementedReturnTypeMismatch>
</file>
<file src="src/Command/Phinx/Seed.php">
<PossiblyNullReference occurrences="1">
<code>setInput</code>
</PossiblyNullReference>
</file>
<file src="src/Shell/Task/SimpleMigrationTask.php">
<PossiblyNullArgument occurrences="1">
<code>$name</code>
</PossiblyNullArgument>
</file>
<file src="src/TableFinderTrait.php">
<InternalClass occurrences="1">
<code>new Filesystem()</code>
</InternalClass>
<InternalMethod occurrences="1">
<code>find</code>
</InternalMethod>
</file>
<file src="src/Util/ColumnParser.php">
<PossiblyNullArgument occurrences="1">
<code>$fieldType</code>
</PossiblyNullArgument>
</file>
<file src="src/Util/UtilTrait.php">
<InvalidReturnType occurrences="1">
<code>string|null</code>
</InvalidReturnType>
</file>
</files>
25 changes: 25 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<psalm
allowStringToStandInForClass="true"
usePhpDocMethodsWithoutMagicCall="true"
ignoreInternalFunctionFalseReturn="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
autoloader="tests/bootstrap.php"
>
<projectFiles>
<directory name="src"/>
</projectFiles>

<issueHandlers>
<RedundantConditionGivenDocblockType errorLevel="suppress"/>
<TypeCoercion errorLevel="suppress"/>
<DocblockTypeContradiction errorLevel="suppress"/>
<MissingClosureParamType errorLevel="suppress"/>
<MissingClosureReturnType errorLevel="suppress"/>
<PropertyNotSetInConstructor errorLevel="suppress"/>
<UnresolvableInclude errorLevel="suppress"/>
</issueHandlers>
</psalm>
9 changes: 7 additions & 2 deletions src/CakeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
*/
class CakeManager extends Manager
{
/**
* @var int
*/
public $maxNameLength = 0;

/**
Expand Down Expand Up @@ -146,7 +149,7 @@ public function migrateToDateTime($environment, \DateTime $dateTime, $fake = fal
/**
* @inheritDoc
*/
public function rollbackToDateTime($environment, \DateTime $dateTime, $force = false)
public function rollbackToDateTime(string $environment, \DateTime $dateTime, bool $force = false): void
{
$env = $this->getEnvironment($environment);
$versions = $env->getVersions();
Expand Down Expand Up @@ -246,6 +249,7 @@ public function getVersionsToMark($input)
return $versions;
}

/** @var string $version */
$version = $targetArg ?: $versionArg;

if ($input->getOption('only') || !empty($versionArg)) {
Expand Down Expand Up @@ -329,6 +333,7 @@ protected function getMigrationClassName($path)
$class = ucwords($class);
$class = str_replace(' ', '', $class);
if (strpos($class, '.') !== false) {
/** @psalm-suppress PossiblyFalseArgument */
$class = substr($class, 0, strpos($class, '.'));
}

Expand Down Expand Up @@ -362,7 +367,7 @@ public function getSeeds()
{
parent::getSeeds();
if (empty($this->seeds)) {
return $this->seeds;
return [];
}

foreach ($this->seeds as $class => $instance) {
Expand Down
3 changes: 3 additions & 0 deletions src/Command/BakeMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
*/
class BakeMigrationCommand extends BakeSimpleMigrationCommand
{
/**
* @var string
*/
protected $_name;

/**
Expand Down
9 changes: 7 additions & 2 deletions src/Command/BakeMigrationDiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class BakeMigrationDiffCommand extends BakeSimpleMigrationCommand
protected $commonTables;

/**
* @inheritDoc
* @var array
*/
protected $templateData = [];

Expand Down Expand Up @@ -151,6 +151,7 @@ protected function setup(Arguments $args)
$migratedItems = [];
if ($tableExists) {
$query = $connection->newQuery();
/** @var array $migratedItems */
$migratedItems = $query
->select(['version'])
->from($this->phinxTable)
Expand Down Expand Up @@ -243,6 +244,7 @@ protected function getColumns()
$addedColumns = array_diff($currentColumns, $oldColumns);
foreach ($addedColumns as $columnName) {
$column = $currentSchema->getColumn($columnName);
/** @var int $key */
$key = array_search($columnName, $currentColumns);
if ($key > 0) {
$column['after'] = $currentColumns[$key - 1];
Expand Down Expand Up @@ -309,6 +311,7 @@ protected function getColumns()
if (!empty($removedColumns)) {
foreach ($removedColumns as $columnName) {
$column = $this->dumpSchema[$table]->getColumn($columnName);
/** @var int $key */
$key = array_search($columnName, $oldColumns);
if ($key > 0) {
$column['after'] = $oldColumns[$key - 1];
Expand Down Expand Up @@ -539,7 +542,9 @@ protected function getCurrentSchema()
}

$connection = ConnectionManager::get($this->connection);
$connection->cacheMetadata(false);
if (method_exists($connection, 'cacheMetadata')) {
$connection->cacheMetadata(false);
}
$collection = $connection->getSchemaCollection();
foreach ($this->tables as $table) {
if (preg_match("/^.*phinxlog$/", $table) === 1) {
Expand Down
3 changes: 3 additions & 0 deletions src/Command/BakeMigrationSnapshotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class BakeMigrationSnapshotCommand extends BakeSimpleMigrationCommand
use TableFinderTrait;
use UtilTrait;

/**
* @var string
*/
protected $_name;

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Command/BakeSeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class BakeSeedCommand extends SimpleBakeCommand
*/
public $pathFragment = 'config/Seeds/';

/**
* @var string
*/
protected $_name;

/**
Expand Down Expand Up @@ -97,15 +100,18 @@ public function templateData(Arguments $arguments): array
$namespace = $this->_pluginNamespace($this->plugin);
}

/** @psalm-suppress PossiblyNullArgument */
$table = Inflector::tableize($arguments->getArgumentAt(0));
if ($arguments->hasOption('table')) {
/** @var string $table */
$table = $arguments->getOption('table');
}

$records = false;
if ($arguments->getOption('data')) {
$limit = (int)$arguments->getOption('limit');

/** @var string $fields */
$fields = $arguments->getOption('fields') ?: '*';
if ($fields !== '*') {
$fields = explode(',', $fields);
Expand All @@ -121,6 +127,7 @@ public function templateData(Arguments $arguments): array
$query->limit($limit);
}

/** @var array $records */
$records = $connection->execute($query->sql())->fetchAll('assoc');
$records = $this->prettifyArray($records);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Command/BakeSimpleMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,15 @@ protected function createFile(string $path, string $contents, Arguments $args, C
* If the name is invalid, the task will exit
*
* @param string|null $name Name for the generated migration
* @return string|null Name of the migration file or null if empty
* @return string Name of the migration file
*/
protected function getMigrationName($name = null)
{
if (empty($name)) {
$this->io->abort('Choose a migration name to bake in CamelCase format');
}

/** @psalm-suppress PossiblyNullArgument */
$name = $this->_getName($name);
$name = Inflector::camelize($name);

Expand Down
5 changes: 4 additions & 1 deletion src/Command/Phinx/CacheBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class CacheBuild extends BaseCommand
use SchemaTrait;

/**
* @inheritDoc
* Configures the current command.
*
* @return void
*/
protected function configure()
{
Expand Down Expand Up @@ -43,6 +45,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var string $name */
$name = $input->getArgument('name');
$schema = $this->_getSchema($input, $output);
if (!$schema) {
Expand Down
5 changes: 4 additions & 1 deletion src/Command/Phinx/CacheClear.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class CacheClear extends BaseCommand
use SchemaTrait;

/**
* @inheritDoc
* Configures the current command.
*
* @return void
*/
protected function configure()
{
Expand Down Expand Up @@ -44,6 +46,7 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$schema = $this->_getSchema($input, $output);
/** @var string $name */
$name = $input->getArgument('name');
if (!$schema) {
return static::CODE_ERROR;
Expand Down
1 change: 1 addition & 0 deletions src/Command/Phinx/CommandTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function bootstrap(InputInterface $input, OutputInterface $output)
$this->setManager(new CakeManager($this->getConfig(), $input, $output));
}
/** @var \Phinx\Migration\Manager\Environment $env */
/** @psalm-suppress PossiblyNullReference */
$env = $this->getManager()->getEnvironment('default');
$adapter = $env->getAdapter();
if (!$adapter instanceof CakeAdapter) {
Expand Down
5 changes: 4 additions & 1 deletion src/Command/Phinx/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class Create extends CreateCommand
use ConfigurationTrait;

/**
* @inheritDoc
* Configures the current command.
*
* @return void
*/
protected function configure()
{
Expand Down Expand Up @@ -76,6 +78,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$migrationPaths = $this->getConfig()->getMigrationPaths();
$migrationPath = array_pop($migrationPaths) . DS;
/** @var string $name */
$name = $input->getArgument('name');
[$phinxTimestamp, $phinxName] = explode('_', Util::mapClassNameToFileName($name), 2);
$migrationFilename = glob($migrationPath . '*' . $phinxName);
Expand Down
5 changes: 4 additions & 1 deletion src/Command/Phinx/Dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class Dump extends AbstractCommand
protected $output;

/**
* @inheritDoc
* Configures the current command.
*
* @return void
*/
protected function configure()
{
Expand Down Expand Up @@ -83,6 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->output($output);

$path = $this->getOperationsPath($input);
/** @var string $connectionName */
$connectionName = $input->getOption('connection') ?: 'default';
$connection = ConnectionManager::get($connectionName);
$collection = $connection->getSchemaCollection();
Expand Down
14 changes: 8 additions & 6 deletions src/Command/Phinx/MarkMigrated.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public function output(?OutputInterface $output = null)
}

/**
* @inheritDoc
* Configures the current command.
*
* @return void
*/
protected function configure()
{
Expand Down Expand Up @@ -149,7 +151,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function isUsingDeprecatedAll()
{
$version = $this->input->getArgument('version');
$version = $this->input()->getArgument('version');

return $version === 'all' || $version === '*';
}
Expand All @@ -161,7 +163,7 @@ protected function isUsingDeprecatedAll()
*/
protected function hasExclude()
{
return $this->input->getOption('exclude');
return (bool)$this->input()->getOption('exclude');
}

/**
Expand All @@ -171,7 +173,7 @@ protected function hasExclude()
*/
protected function hasOnly()
{
return $this->input->getOption('only');
return (bool)$this->input()->getOption('only');
}

/**
Expand All @@ -181,7 +183,7 @@ protected function hasOnly()
*/
protected function isUsingDeprecatedVersion()
{
$version = $this->input->getArgument('version');
$version = $this->input()->getArgument('version');

return $version && $version !== 'all' && $version !== '*';
}
Expand All @@ -195,7 +197,7 @@ protected function invalidOnlyOrExclude()
{
return ($this->hasExclude() && $this->hasOnly()) ||
($this->hasExclude() || $this->hasOnly()) &&
$this->input->getOption('target') === null;
$this->input()->getOption('target') === null;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Command/Phinx/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class Migrate extends MigrateCommand
use EventDispatcherTrait;

/**
* @inheritDoc
* Configures the current command.
*
* @return void
*/
protected function configure()
{
Expand Down
Loading

0 comments on commit e47728b

Please sign in to comment.