Skip to content
Merged
20 changes: 20 additions & 0 deletions crates/cli/tests/integration/invalid_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ fn warnings_under(report: &Value, key: &str) -> Vec<String> {
.unwrap_or_default()
}

#[test]
fn check_reports_invalid_nested_composer_path_repository() {
let repo = init_git_repo(&[
(
"app/composer.json",
r#"{"repositories":[{"type":"path","url":"../packages/["}]}"#,
),
("app/main.php", "<?php function run(): int { return 1; }"),
]);

let report = check_report(repo.path());
let [warning]: [String; 1] = warnings(&report).try_into().expect("one manifest warning");
assert_contains(
&warning,
"Skipped the invalid php manifest app/composer.json",
);
assert_contains(&warning, "is not a valid glob");
assert_eq!(report["files"], 1);
}

#[test]
fn check_skips_invalid_go_modules_with_warnings_and_measures_the_rest() {
let repo = monorepo_with_invalid_go_modules();
Expand Down
123 changes: 123 additions & 0 deletions crates/cli/tests/integration/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,126 @@ fn trace_connects_rust_path_dependency_for_default_and_custom_lib_paths() {
assert_eq!(parsed["paths"], json!([["src/lib.rs", dep_lib_path]]));
}
}

#[test]
fn trace_resolves_php_psr4_in_the_importing_application() {
let manifest = r#"{"autoload":{"psr-4":{"App\\":"src/"}}}"#;
let service = r"<?php namespace App; class Service {}";
let repo = init_git_repo(&[
("a/composer.json", manifest),
("a/src/Service.php", service),
("b/composer.json", manifest),
("b/main.php", r"<?php use App\Service; new Service();"),
("b/src/Service.php", service),
]);

assert_eq!(
[
trace_text(repo.path(), &["b/main.php", "b/src/Service.php"]),
trace_text(repo.path(), &["b/main.php", "a/src/Service.php"]),
],
[
"b/main.php -> b/src/Service.php",
"No runtime import path from b/main.php to a/src/Service.php",
],
);
}

const COMPOSER_SERVICE_MANIFEST: &str = r#"{"name":"demo/lib","require":{"demo/shared":"*"},"autoload":{"psr-4":{"Demo\\Lib\\":"src/"}},"autoload-dev":{"psr-4":{"Demo\\Tests\\":"tests/"}}}"#;
const PHP_SERVICE: &str = r"<?php namespace Demo\Lib; use Demo\Shared\Helper; class Service {}";

#[test]
fn trace_resolves_php_psr4_through_declared_path_dependencies() {
let repo = init_git_repo(&[
(
"app/composer.json",
r#"{"repositories":[{"type":"path","url":"../packages/lib"},{"type":"path","url":"../packages/*"}],"require-dev":{"demo/lib":"*"}}"#,
),
(
"app/main.php",
r"<?php use Demo\Lib\Service; use Demo\Other\Extra; use Demo\Tests\Fixture;",
),
("packages/lib/composer.json", COMPOSER_SERVICE_MANIFEST),
("packages/lib/src/Service.php", PHP_SERVICE),
(
"packages/lib/tests/Fixture.php",
r"<?php namespace Demo\Tests; class Fixture {}",
),
(
"packages/shared/composer.json",
r#"{"name":"demo/shared","autoload":{"psr-4":{"Demo\\Shared\\":"src/"}}}"#,
),
(
"packages/shared/src/Helper.php",
r"<?php namespace Demo\Shared; class Helper {}",
),
(
"packages/other/composer.json",
r#"{"name":"demo/other","autoload":{"psr-4":{"Demo\\Other\\":"src/"}}}"#,
),
(
"packages/other/src/Extra.php",
r"<?php namespace Demo\Other; class Extra {}",
),
(
"packages/a-shadow/composer.json",
r#"{"name":"demo/lib","autoload":{"psr-4":{"Demo\\Lib\\":"src/"}}}"#,
),
(
"packages/a-shadow/src/Service.php",
r"<?php namespace Demo\Lib; class Service {}",
),
]);

for (target, expected_paths) in [
(
"packages/lib/src/Service.php",
json!([["app/main.php", "packages/lib/src/Service.php"]]),
),
(
"packages/shared/src/Helper.php",
json!([[
"app/main.php",
"packages/lib/src/Service.php",
"packages/shared/src/Helper.php"
]]),
),
("packages/other/src/Extra.php", json!([])),
("packages/lib/tests/Fixture.php", json!([])),
("packages/a-shadow/src/Service.php", json!([])),
] {
let parsed = trace_json(repo.path(), &["app/main.php", target]);
assert_eq!(parsed["paths"], expected_paths, "trace to {target}");
}
}

#[test]
fn trace_keeps_php_psr4_targets_from_each_consuming_application() {
let app = r"<?php use Demo\Lib\Service; new Service();";
let manifest = r#"{"repositories":[{"type":"path","url":"../lib"},{"type":"path","url":"./shared"}],"require":{"demo/lib":"*"}}"#;
let helper = r"<?php namespace Demo\Shared; class Helper {}";
let package = r#"{"name":"demo/shared","autoload":{"psr-4":{"Demo\\Shared\\":"src/"}}}"#;
let repo = init_git_repo(&[
("a/composer.json", manifest),
("a/main.php", app),
("a/shared/composer.json", package),
("a/shared/src/Helper.php", helper),
("b/composer.json", manifest),
("b/main.php", app),
("b/shared/composer.json", package),
("b/shared/src/Helper.php", helper),
("lib/composer.json", COMPOSER_SERVICE_MANIFEST),
("lib/src/Service.php", PHP_SERVICE),
]);

for (importer, target) in [
("a/main.php", "a/shared/src/Helper.php"),
("b/main.php", "b/shared/src/Helper.php"),
] {
let parsed = trace_json(repo.path(), &[importer, target]);
assert_eq!(
parsed["paths"],
json!([[importer, "lib/src/Service.php", target]])
);
}
}
1 change: 1 addition & 0 deletions docs/guide/languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Every built-in Language plugin extracts:
| `shell` | `.sh`, `.bash`, `.zsh` | none (no imports; joins enclosing Workspace package) | `test/`, `tests/`, `spec/`, `test_*.sh`, `*_test.sh`; `ltmain.sh`, `configure.sh`, generated-file headers -> Generated; POSIX `sh`, Bash, or Zsh from the shebang or extension; 10 Hazard line rules; see [Shell](./shell.md) |

Core rules also classify files under `vendor/` or `third_party/` as `vendored` and files with generated-file header comments (`@generated`, `GENERATED CODE`, `DO NOT EDIT`) as `generated`.
See [`codeint trace`](../reference.md#codeint-trace) for how codeint resolves PHP PSR-4 imports across Composer packages.
See [Go](./go.md) for Go terminology mapping, multi-file Go package imports, entry points, Flagged line rules, and `go.mod` Dependency hygiene.
See [SQL](./sql.md) for generic SQL measurement, Dialects, templated SQL and rejected statements, automatic SQL schemas, and [Hazards](./hazards.md) for the Hazards Metric.
See [Shell](./shell.md) for shell flavor detection, top-level `<script>` functions, Flagged line rules, and Hazard line rules.
Expand Down
2 changes: 2 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,8 @@ Literal `require()` calls and TypeScript import assignments with `require()` mat
| `-h, --help` | Print the help of `codeint trace` |

`codeint trace` finds the shortest runtime import path connecting two measured files in the working tree.
PHP PSR-4 imports follow the closest `composer.json` and its declared local Composer path dependencies.
When applications resolve a shared package's import to different files, codeint keeps all of those targets.
When `<FROM>` or `<TO>` is a Production file that its Language plugin marks as a program start, the report names `"program-start mark"` as the entry-point reason in `from_entry_point` or `to_entry_point` (and `Entry point: <path> (program-start mark)` in `text` format).
When a step on a traced path comes from one import that leads to several files, the report lists that import and every file it leads to in `multi_file_imports` (and `Import <specifier> in <importer> leads to: <targets>` in `text` format).
When no runtime import path connects them, it reports `connected: false` and exits with code 0:
Expand Down
9 changes: 9 additions & 0 deletions docs/verification/features/trace.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ docs/verification/verify.sh assert "$RUN_DIR" trace

Assert checks connected, multi-path, and disconnected payloads, `--output` file creation with empty stdout, error exits, and `FIXTURE_REPO` cleanliness.

Run the PHP CLI integration cases to check Composer PSR-4 imports across applications and local dependencies:

```sh
cargo test --locked -p codeint --test integration php_psr4
```

The application fixture reports `b/main.php -> b/src/Service.php` and `No runtime import path from b/main.php to a/src/Service.php`.
The shared package fixture also traces the import to both applications' targets.

## Evidence and Cleanup

Evidence files are stored under `$EVIDENCE_DIR/trace/` and `$EVIDENCE_DIR/verdict.json`.
Expand Down
6 changes: 5 additions & 1 deletion plugins/languages/php/HOW-TO.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
## Imports and PSR-4 resolution

- `src/imports.rs` and `src/resolution.rs` extract top-level `use` statements (including grouped and aliased imports) and static relative `require`/`include` paths.
They resolve PSR-4 namespaces through `composer.json` `autoload.psr-4` and `autoload-dev.psr-4`, falling back to declared Composer sub-packages when no local file or directory matches.
See [PHP PSR-4 under `codeint trace`](../../../docs/reference.md#codeint-trace) for how codeint scopes imports.
A Workspace package can also resolve its declared local dependencies through its nearest enclosing Composer workspace's `path` repositories.
The importer uses its own `autoload.psr-4` and `autoload-dev.psr-4` rules.
Each reachable local dependency contributes only `autoload.psr-4` and can reach other local packages through `require`.
When a PSR-4 prefix matches and no local file or directory matches, resolution falls back to declared Composer sub-packages.
They classify PHP built-in classes, PHP 8.x built-in extension namespaces, Composer runtime API namespaces (`Composer\InstalledVersions`, `Composer\Autoload\...`), and single-segment global symbols as `Builtin`.
Multi-segment external namespaces match against the repository's declared `composer.json` dependencies across 2-segment, 3-segment joined, PSR-17 factory and 3-segment class-word forms, framework structural sub-layer (`Component`, `Contracts`, `Bridge`, `Bundle`, `Common`, `UX`), and unique multi-word package-name forms before falling back to `vendor/package` normalization.
- `src/dependencies.rs` parses `composer.json` `require` (`Runtime`), `require-dev` (`Dev`), and `suggest` (`Optional`) dependencies per Workspace package while excluding platform requirements (`php`, `ext-*`, `lib-*`) and local workspace package names.
Expand Down
Loading