Extract classes, functions, methods, namespace uses, and calls from a .php file. Also parses any inline `` `` blocks as JavaScript (#2320): the PHP grammar sees those spans as opaque markup, so a page mixing PHP with client-side JS previously indexed only its PHP half. The JS pas
(path: Path)
| 4909 | |
| 4910 | |
| 4911 | def extract_php(path: Path) -> dict: |
| 4912 | """Extract classes, functions, methods, namespace uses, and calls from a .php file. |
| 4913 | |
| 4914 | Also parses any inline ``<script>`` blocks as JavaScript (#2320): the PHP |
| 4915 | grammar sees those spans as opaque markup, so a page mixing PHP with |
| 4916 | client-side JS previously indexed only its PHP half. The JS pass masks |
| 4917 | everything else to blanks (preserving line numbers) and reuses the same |
| 4918 | file node id, so JS symbols land under the one file node like any other |
| 4919 | PHP symbol; a same-name PHP/JS collision (rare — different languages, |
| 4920 | same file) is resolved by keeping the PHP node, since PHP is the file's |
| 4921 | primary language, and dropping every JS edge that touches the id the |
| 4922 | dropped JS node would have used, so no edge from the discarded symbol |
| 4923 | is left to silently attach to the unrelated PHP node sharing its id. |
| 4924 | """ |
| 4925 | try: |
| 4926 | source = _php_blank_unparsed_casts(_read_source_bytes(path)) |
| 4927 | except Exception: |
| 4928 | source = None # let _extract_generic read the file and report errors |
| 4929 | # Every call site, not one per pair: the build folds repeats into `also_seen`. |
| 4930 | result = _extract_generic(path, _PHP_CONFIG, source_override=source, preserve_call_sites=True) |
| 4931 | try: |
| 4932 | src = _read_source_text(path, warn=False) |
| 4933 | masked, had_script = _php_mask_to_script_blocks(src) |
| 4934 | if not had_script: |
| 4935 | return result |
| 4936 | js_result = _extract_generic(path, _JS_CONFIG, source_override=masked.encode("utf-8")) |
| 4937 | # The file node itself always collides by construction -- both passes |
| 4938 | # derive its id from the same path, on purpose, so JS symbols can |
| 4939 | # land under the one PHP file node. That expected collision must not |
| 4940 | # be treated as a dropped-symbol collision below, or every JS edge |
| 4941 | # sourced from the file node (i.e. every top-level JS symbol's own |
| 4942 | # `contains` edge) would be discarded along with it. |
| 4943 | file_node_id = result["nodes"][0]["id"] if result.get("nodes") else None |
| 4944 | existing_ids = {n["id"] for n in result.get("nodes", [])} |
| 4945 | colliding_ids: set = set() |
| 4946 | for n in js_result.get("nodes", []): |
| 4947 | if n["id"] in existing_ids: |
| 4948 | if n["id"] != file_node_id: |
| 4949 | colliding_ids.add(n["id"]) |
| 4950 | continue |
| 4951 | result.setdefault("nodes", []).append(n) |
| 4952 | existing_ids.add(n["id"]) |
| 4953 | for e in js_result.get("edges", []): |
| 4954 | # A JS node dropped above for colliding with an existing PHP |
| 4955 | # node id must not leave its edges behind -- otherwise an edge |
| 4956 | # meant for the dropped JS symbol silently attaches to the |
| 4957 | # unrelated retained PHP node that happens to share its id. |
| 4958 | if e.get("source") in colliding_ids or e.get("target") in colliding_ids: |
| 4959 | continue |
| 4960 | result.setdefault("edges", []).append(e) |
| 4961 | except Exception: |
| 4962 | pass |
| 4963 | return result |
| 4964 | |
| 4965 | |
| 4966 | # One level of balanced parens (e.g. `Foo #(Bar #(int))`) — bounded so malformed |