Skip to content

Add middlewares into prependToGroup - #74

Open
AZabolotnikov wants to merge 19 commits into
masterfrom
add-midlewares-prepend-group
Open

Add middlewares into prependToGroup#74
AZabolotnikov wants to merge 19 commits into
masterfrom
add-midlewares-prepend-group

Conversation

@AZabolotnikov

@AZabolotnikov AZabolotnikov commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

refs: #73

@AZabolotnikov AZabolotnikov self-assigned this Jun 8, 2026
Copilot AI review requested due to automatic review settings June 8, 2026 06:00

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2b6ac06670

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php Outdated

protected function updateMiddlewareGroupStatement(Closure $closure, int $groupIndex): void
{
$originalMiddlewareList = $closure->stmts[$groupIndex]->expr->args[1]->value->items;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle existing single middleware values

If an existing bootstrap file uses the single-middleware form for this Laravel API, e.g. $middleware->prependToGroup('api', SomeMiddleware::class) or a string, this path dereferences ->items as if the second argument were always an array. Updating that group will then fail instead of normalizing the existing value and appending the new middleware.

Useful? React with 👍 / 👎.

Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support in AppBootstrapBuilder for inserting/updating Laravel withMiddleware(...)->prependToGroup(...) statements in bootstrap/app.php, with fixtures/tests to validate output.

Changes:

  • Introduces AddMiddlewarePrependToGroup visitor and exposes it via AppBootstrapBuilder::addMiddlewarePrependToGroup().
  • Adds PHPUnit coverage plus new fixtures/origin structures for prepend-to-group scenarios.
  • Updates docker-compose.yml to map host.docker.internal to host-gateway.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php New AST visitor to add/merge prependToGroup() calls inside withMiddleware() closure.
src/Builders/AppBootstrapBuilder.php Adds builder API addMiddlewarePrependToGroup() and import handling for class-based middleware.
tests/AppBootstrapBuilderTest.php Adds tests covering new prepend-to-group behavior.
tests/Support/Classes/FakeClass.php Test-only middleware class used in fixtures.
tests/Support/OriginStructures/bootstrap_with_prepend_group.php New origin structure input for tests.
tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group.php Expected output fixture for newly generated prepend-to-group calls.
tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_changed_prepend_group.php Expected output fixture when merging into existing prepend-to-group call.
src/Visitors/AppBootstrapVisitors/AbstractAppBootstrapVisitor.php Minor change to typed class constant declaration.
docker-compose.yml Adds extra_hosts mapping for host.docker.internal.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php
Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php
Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php Outdated
Comment thread tests/AppBootstrapBuilderTest.php
Comment thread tests/AppBootstrapBuilderTest.php
Comment thread tests/AppBootstrapBuilderTest.php

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 682a3ddf1b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php Outdated
{
$middlewareList = $this->getMiddlewareList();

$methodCall = new MethodCall(new Variable('middleware'), new Identifier($this->targetMethod), [

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use the closure's middleware variable name

If the bootstrap file names the withMiddleware callback parameter anything other than $middleware, for example function (Middleware $m): void, this inserts $middleware->prependToGroup(...) and leaves the generated bootstrap code referencing an undefined variable. The visitor already has the closure in insertNode, so the new method call should be built with the actual first parameter name instead of a hard-coded one.

Useful? React with 👍 / 👎.

@coveralls

coveralls commented Jun 8, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 27944873134

Coverage increased (+0.9%) to 99.549%

Details

  • Coverage increased (+0.9%) from the base build.
  • Patch coverage: 61 of 61 lines across 2 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 443
Covered Lines: 441
Line Coverage: 99.55%
Coverage Strength: 10.32 hits per line

💛 - Coveralls

@AZabolotnikov AZabolotnikov changed the title Add midlewares prepend group Add middlewares into prependTogroup Jun 8, 2026
@AZabolotnikov AZabolotnikov changed the title Add middlewares into prependTogroup Add middlewares into prependToGroup Jun 8, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e49fac41fb

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

if (class_exists($middleware)) {
$basename = class_basename($middleware);

$value = new ClassConstFetch(new Name($basename), 'class');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid unaliased middleware basenames

When the bootstrap file already imports a different class with the same short name as the middleware being added, this drops the namespace and later adds another unaliased use, so the generated file either has a duplicate import name or FakeClass::class resolves to the wrong class. This can happen with common middleware basenames such as Authenticate; use a fully-qualified Name here or add aliasing before shortening the class name.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6f0e82bb7b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8b80c3c90e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php Outdated
Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php Outdated

@artengin artengin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Don't forget to add information about the new method to the README documentation.
  2. The task requirements mention a third argument, position, but this has not been implemented yet. Please cover these cases with tests.
  3. At the moment, creating the withMiddleware method when it is not present in the original file is not implemented. Please cover this case with tests.

Comment thread tests/Support/OriginStructures/bootstrap_with_prepend_group.php Outdated
Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php
Comment thread tests/AppBootstrapBuilderTest.php Outdated
@artengin artengin assigned AZabolotnikov and unassigned artengin Jun 12, 2026
@DenTray

DenTray commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator
  1. At the moment, creating the withMiddleware method when it is not present in the original file is not implemented. Please cover this case with tests.

@artengin @AZabolotnikov let's also implement as a separate PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c084939a1d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".


protected function findMiddlewareGroupIndex(array $stmts): ?int
{
return array_find_key($stmts, function (Expression $stmt) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check every existing group call before appending

When a bootstrap already has more than one prependToGroup('api', ...) call, this returns only the first match, so mergeMiddlewares() checks duplicates against that first argument list only. If the middleware being added is already present in a later call for the same group, the visitor appends it to the first call as well and the generated bootstrap runs the same middleware twice; scan all matching calls, or consolidate them, before deciding the middleware is new.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 953dc6297e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cbc208f5d2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php
Comment thread README.md Outdated
->save();
}

public function testAddMiddlewarePrependToGroupExistsMiddlewares()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test isn't quite correct: it doesn't actually change the contents of prependToGroup, but it still adds an unnecessary import.
If the intent was to show that duplicate code isn't created inside prependToGroup, the fixture should have been named bootstrap_without_changed_prepend_group, and the import shouldn't appear either.

I suggest we either:

  1. In a follow-up PR, figure out how to avoid importing a class when no changes were made to PrependToGroup (we'll need this capability in the future anyway), or
  2. Simply rework the fixture and test to properly demonstrate that duplicates aren't created and the fixture remains unchanged.

Comment thread tests/AppBootstrapBuilderTest.php
Comment thread src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php Outdated
@artengin artengin assigned AZabolotnikov and unassigned artengin Jun 19, 2026
AZabolotnikov and others added 2 commits June 22, 2026 12:04
Co-authored-by: Artyom Osepyan <152782500+artengin@users.noreply.github.com>
Co-authored-by: Artyom Osepyan <152782500+artengin@users.noreply.github.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fe4b39d031

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +107 to +108
return $originalName === $newMiddleware
|| $originalName === class_basename($newMiddleware);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Resolve class constants before de-duplicating

When the target group already contains an imported class with the same short name as the middleware being added, this basename comparison treats different classes as duplicates. For example, if the bootstrap imports App\Http\Middleware\Authenticate and the group contains Authenticate::class, adding Illuminate\Auth\Middleware\Authenticate::class is skipped even though it resolves to a different middleware, so the requested middleware is omitted from the saved file; compare against resolved FQCNs instead of only class_basename().

Useful? React with 👍 / 👎.

@artengin artengin assigned DenTray and unassigned artengin Jun 22, 2026
AZabolotnikov added a commit that referenced this pull request Jun 24, 2026
AZabolotnikov added a commit that referenced this pull request Jun 24, 2026
AZabolotnikov added a commit that referenced this pull request Jul 10, 2026
AZabolotnikov added a commit that referenced this pull request Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants