Skip to content

Commit

Permalink
Fix up new model annotations as generics (#335)
Browse files Browse the repository at this point in the history
* Fix up new model annotations as generics

* Add feature switch
  • Loading branch information
dereuromark authored Dec 4, 2023
1 parent 37558ba commit 33d8f43
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 24 deletions.
1 change: 1 addition & 0 deletions config/app.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'arrayAsGenerics' => false, // Enable to have modern generics syntax (recommended) in doc blocks
// Set to false to disable
'objectAsGenerics' => false, // Enable to have modern generics syntax (recommended) in doc blocks
'assocsAsGenerics' => false, // Enable to have modern generics syntax (NOT recommended yet) in doc blocks
// Set to false to disable, or string if you have a custom FQCN to be used
'templateCollectionObject' => true,
// Set to false to disable, defaults to mixed if enabled, you can also pass callable for logic
Expand Down
4 changes: 2 additions & 2 deletions docs/Annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ A LocationsTable class would then get the following doc block annotations added
* @method \Cake\Datasource\ResultSetInterface<\App\Model\Entity\Location>|false deleteMany(iterable $entities, $options = [])
* @method \Cake\Datasource\ResultSetInterface<\App\Model\Entity\Location> deleteManyOrFail(iterable $entities, $options = [])
*
* @property \App\Model\Table\ImagesTable&\Cake\ORM\Association\HasMany $Images
* @property \App\Model\Table\UsersTable&\Cake\ORM\Association\BelongsTo $Users
* @property \Cake\ORM\Association\HasMany<\App\Model\Table\ImagesTable> $Images
* @property \Cake\ORM\Association\BelongsTo<\App\Model\Table\UsersTable> $Users
*
* @mixin \Cake\ORM\Behavior\TimestampBehavior
```
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ parameters:
bootstrapFiles:
- tests/bootstrap.php
- tests/shim.php
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
- '#Unsafe usage of new static\(\).+#'
- '#Parameter \#1 \$object of function get_class expects object, object\|string given.#'
6 changes: 5 additions & 1 deletion src/Annotator/ModelAnnotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ protected function buildAnnotations(array $associations, string $entity, array $
$annotations = [];
foreach ($associations as $type => $assocs) {
foreach ($assocs as $name => $className) {
$annotations[] = "@property \\{$className}&\\{$type} \${$name}";
if (Configure::read('IdeHelper.assocsAsGenerics') === true) {
$annotations[] = "@property \\{$type}<\\{$className}> \${$name}";
} else {
$annotations[] = "@property \\{$className}&\\{$type} \${$name}";
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/Annotator/ModelAnnotatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace IdeHelper\Test\TestCase\Annotator;

use Cake\Console\ConsoleIo;
use Cake\Core\Configure;
use Cake\Database\Schema\TableSchema;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -42,6 +43,8 @@ protected function setUp(): void {
$consoleIo = new ConsoleIo($this->out, $this->err);
$this->io = new Io($consoleIo);

Configure::write('IdeHelper.assocsAsGenerics', true);

$x = TableRegistry::getTableLocator()->get('IdeHelper.Foos', ['className' => FoosTable::class]);
$columns = [
'id' => [
Expand Down Expand Up @@ -90,6 +93,15 @@ protected function setUp(): void {
TableRegistry::getTableLocator()->set('Foos', $x);
}

/**
* @return void
*/
public function tearDown(): void {
parent::tearDown();

Configure::delete('IdeHelper.assocsAsGenerics');
}

/**
* @return void
*/
Expand Down
24 changes: 18 additions & 6 deletions tests/TestCase/Command/AnnotateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace IdeHelper\Test\TestCase\Command;

use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
use Cake\Core\Configure;
use Cake\TestSuite\TestCase;
use IdeHelper\Command\AnnotateCommand;

Expand All @@ -26,6 +27,17 @@ protected function setUp(): void {
if (!is_dir(LOGS)) {
mkdir(LOGS, 0770, true);
}

Configure::write('IdeHelper.assocsAsGenerics', true);
}

/**
* @return void
*/
public function tearDown(): void {
parent::tearDown();

Configure::delete('IdeHelper.assocsAsGenerics');
}

/**
Expand Down Expand Up @@ -122,7 +134,7 @@ public function testAll() {
*/
public function testAllCiModeNoChanges() {
$this->exec('annotate all -d -v --ci -p Awesome');
$this->assertExitSuccess();
$this->assertExitSuccess($this->_out->output());
}

/**
Expand All @@ -131,7 +143,7 @@ public function testAllCiModeNoChanges() {
public function testAllCiModeChanges() {
$this->exec('annotate all -d -v --ci');

$this->assertExitCode(AnnotateCommand::CODE_CHANGES);
$this->assertExitCode(AnnotateCommand::CODE_CHANGES, $this->_out->output());
}

/**
Expand All @@ -154,11 +166,11 @@ public static function provideSubcommandsForCiModeTest() {
* @param string $subcommand The subcommand to be tested
* @return void
*/
public function testIndividualSubcommandCiModeNoChanges($subcommand) {
public function testIndividualSubcommandCiModeNoChanges(string $subcommand): void {
$this->skipIf($subcommand === 'view', 'View does not support the plugin parameter');

$this->exec('annotate ' . $subcommand . ' -d -v --ci -p Awesome');
$this->assertExitSuccess();
$this->assertExitSuccess($this->_out->output());
}

/**
Expand All @@ -167,10 +179,10 @@ public function testIndividualSubcommandCiModeNoChanges($subcommand) {
* @param string $subcommand The subcommand to be tested
* @return void
*/
public function testIndividualSubcommandCiModeChanges($subcommand) {
public function testIndividualSubcommandCiModeChanges(string $subcommand): void {
$this->exec('annotate ' . $subcommand . ' -d -v --ci');

$this->assertExitCode(AnnotateCommand::CODE_CHANGES);
$this->assertExitCode(AnnotateCommand::CODE_CHANGES, $this->_out->output());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Cake\ORM\Table;

/**
* @property \Awesome\Model\Table\WindowsTable&\Cake\ORM\Association\HasMany $Windows
* @property \Cake\ORM\Association\HasMany<\Awesome\Model\Table\WindowsTable> $Windows
*/
class HousesTable extends Table {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Cake\ORM\Table;

/**
* @property \Awesome\Model\Table\HousesTable&\Cake\ORM\Association\BelongsTo $Houses
* @property \Cake\ORM\Association\BelongsTo<\Awesome\Model\Table\HousesTable> $Houses
*/
class WindowsTable extends Table {

Expand Down
4 changes: 2 additions & 2 deletions tests/test_app/src/Model/Table/SkipSomeTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use Cake\ORM\Table;

/**
* @property \TestApp\Model\Table\CarsTable|\Cake\ORM\Association\BelongsTo $Cars
* @property \TestApp\Model\Table\CarsTable|\Cake\ORM\Association\BelongsTo $CarsAwesome
* @property \Cake\ORM\Association\BelongsTo<\TestApp\Model\Table\CarsTable> $Cars
* @property \Cake\ORM\Association\BelongsTo<\TestApp\Model\Table\CarsTable> $CarsAwesome
*/
class SkipSomeTable extends Table {

Expand Down
2 changes: 1 addition & 1 deletion tests/test_app/src/Model/Table/WheelsExtraTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Cake\ORM\Table;

/**
* @property \TestApp\Model\Table\CarsOldTable&\Cake\ORM\Association\BelongsTo $Cars
* @property \Cake\ORM\Association\BelongsTo<\TestApp\Model\Table\CarsOldTable> $Cars
*/
class WheelsExtraTable extends Table {

Expand Down
6 changes: 3 additions & 3 deletions tests/test_files/Model/Table/BarBarsAbstractTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
namespace TestApp\Model\Table;

/**
* @property \TestApp\Model\Table\FoosTable&\Cake\ORM\Association\BelongsTo $Foos
* @property \Awesome\Model\Table\HousesTable&\Cake\ORM\Association\BelongsToMany $Houses
* @property \Awesome\Model\Table\WindowsTable&\Cake\ORM\Association\HasMany $Windows
* @property \Cake\ORM\Association\BelongsTo<\TestApp\Model\Table\FoosTable> $Foos
* @property \Cake\ORM\Association\BelongsToMany<\Awesome\Model\Table\HousesTable> $Houses
* @property \Cake\ORM\Association\HasMany<\Awesome\Model\Table\WindowsTable> $Windows
*
* @method \TestApp\Model\Entity\BarBarsAbstract newEmptyEntity()
* @method \TestApp\Model\Entity\BarBarsAbstract newEntity(array $data, array $options = [])
Expand Down
6 changes: 3 additions & 3 deletions tests/test_files/Model/Table/BarBarsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
use Cake\ORM\Table;

/**
* @property \TestApp\Model\Table\FoosTable&\Cake\ORM\Association\BelongsTo $Foos
* @property \Awesome\Model\Table\HousesTable&\Cake\ORM\Association\BelongsToMany $Houses
* @property \Awesome\Model\Table\WindowsTable&\Cake\ORM\Association\HasMany $Windows
* @property \Cake\ORM\Association\BelongsTo<\TestApp\Model\Table\FoosTable> $Foos
* @property \Cake\ORM\Association\BelongsToMany<\Awesome\Model\Table\HousesTable> $Houses
* @property \Cake\ORM\Association\HasMany<\Awesome\Model\Table\WindowsTable> $Windows
*
* @method \TestApp\Model\Entity\BarBar newEmptyEntity()
* @method \TestApp\Model\Entity\BarBar newEntity(array $data, array $options = [])
Expand Down
4 changes: 2 additions & 2 deletions tests/test_files/Model/Table/SkipSomeTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use Cake\ORM\Table;

/**
* @property \TestApp\Model\Table\CarsTable|\Cake\ORM\Association\BelongsTo $Cars
* @property \TestApp\Model\Table\CarsTable|\Cake\ORM\Association\BelongsTo $CarsAwesome
* @property \Cake\ORM\Association\BelongsTo<\TestApp\Model\Table\CarsTable> $Cars
* @property \Cake\ORM\Association\BelongsTo<\TestApp\Model\Table\CarsTable> $CarsAwesome
*/
class SkipSomeTable extends Table {

Expand Down
2 changes: 1 addition & 1 deletion tests/test_files/Model/Table/WheelsExtraTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Cake\ORM\Table;

/**
* @property \TestApp\Model\Table\CarsTable&\Cake\ORM\Association\BelongsTo $Cars
* @property \Cake\ORM\Association\BelongsTo<\TestApp\Model\Table\CarsTable> $Cars
*/
class WheelsExtraTable extends Table {

Expand Down
2 changes: 1 addition & 1 deletion tests/test_files/Model/Table/WheelsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* @method \TestApp\Model\Entity\Wheel newEntity(array $data, array $options = [])
* @property \TestApp\Model\Table\CarsTable&\Cake\ORM\Association\BelongsTo $Cars
* @property \Cake\ORM\Association\BelongsTo<\TestApp\Model\Table\CarsTable> $Cars
* @method \TestApp\Model\Entity\Wheel newEmptyEntity()
* @method \TestApp\Model\Entity\Wheel[] newEntities(array $data, array $options = [])
* @method \TestApp\Model\Entity\Wheel get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
Expand Down

0 comments on commit 33d8f43

Please sign in to comment.