{"id":21194438,"url":"https://github.com/cspray/annotated-target","last_synced_at":"2025-10-05T19:28:39.125Z","repository":{"id":38241393,"uuid":"500158547","full_name":"cspray/annotated-target","owner":"cspray","description":"A static analysis tool for parsing uses of Attributes in PHP source code.","archived":false,"fork":false,"pushed_at":"2024-10-21T19:00:54.000Z","size":96,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"release-1.x","last_synced_at":"2024-10-22T11:34:26.104Z","etag":null,"topics":["attributes","php8","static-analysis"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cspray.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-05T17:00:35.000Z","updated_at":"2024-06-15T15:13:55.000Z","dependencies_parsed_at":"2024-08-08T16:29:21.150Z","dependency_job_id":"a8f228ca-1a93-4292-b924-d0e32a3e84b0","html_url":"https://github.com/cspray/annotated-target","commit_stats":{"total_commits":11,"total_committers":2,"mean_commits":5.5,"dds":0.09090909090909094,"last_synced_commit":"8d1a59725df30a43f27bbdb973c0f714a8eb44f5"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fannotated-target","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fannotated-target/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fannotated-target/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fannotated-target/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cspray","download_url":"https://codeload.github.com/cspray/annotated-target/tar.gz/refs/heads/release-1.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225616382,"owners_count":17497158,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attributes","php8","static-analysis"],"created_at":"2024-11-20T19:22:11.257Z","updated_at":"2025-10-05T19:28:39.009Z","avatar_url":"https://github.com/cspray.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Annotated Target\n\nA static analysis tool for PHP 8 to gather all the places Attributes are used in a given codebase. In short, give this tool a set of directories, and we'll provide a Generator that yields information wherever an Attribute is found. You can filter by Attribute types to get info for just the things you need. Check out the Quick Start below for more info.\n\n## Install\n\nUse [Composer]() to install this package.\n\n```php\ncomposer require cspray/annotated-target\n```\n\n## Quick Start\n\nThe core concept in Annotated Target is an interface called `AnnotatedTarget` that tracks information about each place an Attribute is used in your codebase. It provides three pieces of data:\n\n1. The `Reflector` that the Attribute was targeting. For example, if the Attribute was found targeting a class then a `ReflectionClass` would be available.\n2. The `ReflectionAttribute` for the Attribute.\n3. The instance for the given Attribute.\n\nLet's take a look at some annotated code and how you'd get access to the parsed Attributes. We'll assume that this code exists in `__DIR__ . 'src/Foo.php'`.\n\n```php\n\u003c?php declare(strict_types=1);\n\n#[ClassAttr]\nclass Foo {\n\n    #[PropAttr]\n    private string $prop = 'foo';\n\n    #[MethodAttr]\n    public function getProp() : string {\n        return $this-\u003eprop;\n    }\n    \n}\n```\n\nNow, to get access to these Attribute we need to parse the source code. This is accomplished using the `Cspray\\AnnotatedTarget\\parseAttributes` function, or you could use the `Cspray\\AnnotatedTarget\\PhpParserAnnotatedTargetParser` directly. The `parseAttributes` function is the recommended way of interacting with this library.\n\nIf you want to get _all_ Attributes you can pass just the directories to scan. The library will take a look at all source files in the directory that end with `.php` and statically analyze them for Attribute uses. The first argument accepts either a string or an array, in case you have more than 1 directory to scan.\n\n```php\n\u003c?php declare(strict_types=1);\n\nuse function Cspray\\AnnotatedTarget\\parseAttributes;\n\n// parseAttributes returns a Generator, iterate over it to retrieve all Attributes found\nforeach (parseAttributes(__DIR__ . '/src') as $annotatedTarget) {\n    // $annotatedTarget is an instanceof AnnotatedTarget\n    // This will be a ReflectionClass, ReflectionProperty, or ReflectionMethod depending on which iteration\n    $target = $annotatedTarget-\u003egetTargetReflection();\n    // This will be a ReflectionAttribute\n    $attributeReflection = $annotatedTarget-\u003egetAttributeReflection();\n    // This will be an instance of the Attribute returned from $this-\u003egetAttributeReflection()-\u003enewInstance()\n    $attributeInstance = $annotatedTarget-\u003egetAttributeInstance();\n    \n    // All the methods above are shared\n    $isShared = $annotatedTarget-\u003egetTargetReflection() === $annotatedTarget-\u003egetTargetReflection(); // true\n}\n```\n\nIf you only care about certain Attributes you can pass a second argument to `parseAttributes`, an array of Attribute types that we should filter by. For example, to get only the `#[MethodAttr]` Attribute we'd run the following.\n\n```php\n\u003c?php declare(stric_types=1)\n\nuse function Cspray\\AnnotatedTarget\\parseAttributes;\n\nforeach (parseAttributes(__DIR__ . '/src', [MethodAttr::class]) as $target) {\n    // Only targets for usages of MethodAttr will be included\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcspray%2Fannotated-target","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcspray%2Fannotated-target","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcspray%2Fannotated-target/lists"}