Skip to content
Closed
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
33 changes: 33 additions & 0 deletions scripts/check-association-types.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -euo pipefail

repo_root="$(cd "$(dirname "$0")/.." && pwd)"
mago="$repo_root/vendor/bin/mago"
diagnostics="$(mktemp /tmp/cakephp-mago-associations.XXXXXX)"

cleanup() {
rm -f "$diagnostics"
}
trap cleanup EXIT

# Forward Mago's workspace/config options so the same assertions can exercise
# the installed preset's fixture or an actual CakePHP checkout.
"$mago" "$@" analyze --reporting-format short \
"$repo_root/tests/consumer/src/AssociationTargetTable.php" \
"$repo_root/tests/consumer/src/AssociationTargetTypes.php"

if "$mago" "$@" analyze --reporting-format short \
"$repo_root/tests/consumer/src/AssociationTargetTable.php" \
"$repo_root/tests/analyzer/association-targets.invalid.php" > "$diagnostics" 2>&1; then
echo "Expected incompatible association target return types to fail analysis." >&2
exit 1
fi

# Each association must resolve to the concrete target, rather than Table or
# mixed. Also reject unexpected errors that could hide a broken test setup.
errors="$(grep -Fc 'error[' "$diagnostics" || true)"
invalid_returns="$(grep -Fc 'error[invalid-return-statement]' "$diagnostics" || true)"
if [[ "$errors" -ne 4 || "$invalid_returns" -ne 4 ]]; then
cat "$diagnostics" >&2
exit 1
fi
4 changes: 4 additions & 0 deletions scripts/check-cakephp-6.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ config="$repo_root/tests/cakephp6/mago.toml"
"$repo_root/vendor/bin/mago" --workspace "$checkout_dir" --config "$config" lint --only \
mago-cakephp/trait-suffix,mago-cakephp/public-method-underscore,mago-cakephp/elseif,mago-cakephp/function-docblock,mago-cakephp/docblock-tag-spacing,mago-cakephp/throws-tag,mago-cakephp/inherit-doc

# Resolve association targets against real CakePHP source without type patches.
bash "$repo_root/scripts/check-association-types.sh" \
--workspace "$checkout_dir" --config "$repo_root/tests/cakephp6/analyzer.toml" --no-extensions

if [[ "$migration_audit" == true ]]; then
echo "Running the full Mago-first preset; findings represent migration work."
"$repo_root/vendor/bin/mago" --workspace "$checkout_dir" --config "$repo_root/cakephp.mago.toml" \
Expand Down
3 changes: 3 additions & 0 deletions scripts/check-consumer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ mago="$repo_root/vendor/bin/mago"
"$mago" --workspace "$repo_root" format --check tests/policy
"$mago" --workspace "$consumer_dir" extension validate
"$mago" --workspace "$consumer_dir" format --check
"$mago" --workspace "$consumer_dir" format --check vendor/cakephp-types.php
"$mago" --workspace "$repo_root" format --check tests/analyzer
"$mago" --workspace "$consumer_dir" lint
bash "$repo_root/scripts/check-association-types.sh" --workspace "$consumer_dir"

# The installed preset must reject fully qualified classes without --only or
# --pedantic, then apply an exact, idempotent native fix. PHPDoc stays unchanged.
Expand Down
49 changes: 49 additions & 0 deletions tests/analyzer/association-targets.invalid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);

namespace App;

use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Association\BelongsToMany;
use Cake\ORM\Association\HasMany;
use Cake\ORM\Association\HasOne;
use Cake\ORM\Table;

final class OtherTargetTable extends Table
{
}

final class InvalidAssociationTargetTypes
{
/**
* @param BelongsTo<AssociationTargetTable> $association
*/
public function belongsTo(BelongsTo $association): OtherTargetTable
{
return $association->getTarget();
}

/**
* @param BelongsToMany<AssociationTargetTable> $association
*/
public function belongsToMany(BelongsToMany $association): OtherTargetTable
{
return $association->getTarget();
}

/**
* @param HasMany<AssociationTargetTable> $association
*/
public function hasMany(HasMany $association): OtherTargetTable
{
return $association->getTarget();
}

/**
* @param HasOne<AssociationTargetTable> $association
*/
public function hasOne(HasOne $association): OtherTargetTable
{
return $association->getTarget();
}
}
5 changes: 5 additions & 0 deletions tests/cakephp6/analyzer.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version = "1.47"
php-version = "8.5"

[source]
includes = ["src"]
1 change: 1 addition & 0 deletions tests/consumer/mago.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ extends = "vendor/cakephp/mago-rules/cakephp.mago.toml"

[source]
paths = ["src", "tests"]
includes = ["vendor/cakephp-types.php"]
10 changes: 10 additions & 0 deletions tests/consumer/src/AssociationTargetTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace App;

use Cake\ORM\Table;

final class AssociationTargetTable extends Table
{
}
44 changes: 44 additions & 0 deletions tests/consumer/src/AssociationTargetTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

namespace App;

use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Association\BelongsToMany;
use Cake\ORM\Association\HasMany;
use Cake\ORM\Association\HasOne;

final class AssociationTargetTypes
{
/**
* @param BelongsTo<AssociationTargetTable> $association
*/
public function belongsTo(BelongsTo $association): AssociationTargetTable
{
return $association->getTarget();
}

/**
* @param BelongsToMany<AssociationTargetTable> $association
*/
public function belongsToMany(BelongsToMany $association): AssociationTargetTable
{
return $association->getTarget();
}

/**
* @param HasMany<AssociationTargetTable> $association
*/
public function hasMany(HasMany $association): AssociationTargetTable
{
return $association->getTarget();
}

/**
* @param HasOne<AssociationTargetTable> $association
*/
public function hasOne(HasOne $association): AssociationTargetTable
{
return $association->getTarget();
}
}
60 changes: 60 additions & 0 deletions tests/consumer/vendor/cakephp-types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);

namespace Cake\ORM {
class Table
{
}

/**
* @mixin Table
*/
abstract class Association
{
public function getTarget(): Table
{
return new Table();
}
}
}

namespace Cake\ORM\Association {
use Cake\ORM\Association;
use Cake\ORM\Table;

/**
* @template T of Table
* @mixin T
* @method T getTarget() Gets the table instance for the target side of the association.
*/
class BelongsTo extends Association
{
}

/**
* @template T of Table
* @mixin T
* @method T getTarget() Gets the table instance for the target side of the association.
*/
class BelongsToMany extends Association
{
}

/**
* @template T of Table
* @mixin T
* @method T getTarget() Gets the table instance for the target side of the association.
*/
class HasMany extends Association
{
}

/**
* @template T of Table
* @mixin T
* @method T getTarget() Gets the table instance for the target side of the association.
*/
class HasOne extends Association
{
}
}
Loading