Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

php-parser

Package Overview
Dependencies
Maintainers
2
Versions
76
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

php-parser

Parse PHP code from JS and returns its AST

  • 3.1.0-beta.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
161K
increased by4.72%
Maintainers
2
Weekly downloads
 
Created

What is php-parser?

The php-parser npm package is a JavaScript library that allows you to parse PHP code into an Abstract Syntax Tree (AST). This can be useful for various tasks such as static analysis, code transformation, and code generation.

What are php-parser's main functionalities?

Parsing PHP Code

This feature allows you to parse PHP code into an Abstract Syntax Tree (AST). The code sample demonstrates how to parse a simple PHP script and output the resulting AST.

const parser = require('php-parser');
const phpParser = new parser({ parser: { extractDoc: true } });
const ast = phpParser.parseCode('<?php echo "Hello, World!"; ?>');
console.log(JSON.stringify(ast, null, 2));

Traversing the AST

This feature allows you to traverse the AST to find specific nodes. The code sample demonstrates how to traverse the AST to find and log echo statements.

const parser = require('php-parser');
const phpParser = new parser({ parser: { extractDoc: true } });
const ast = phpParser.parseCode('<?php echo "Hello, World!"; ?>');

function traverse(node) {
  if (node.kind === 'echo') {
    console.log('Found an echo statement');
  }
  for (let key in node) {
    if (node[key] && typeof node[key] === 'object') {
      traverse(node[key]);
    }
  }
}

traverse(ast);

Modifying the AST

This feature allows you to modify the AST. The code sample demonstrates how to change the output of an echo statement from 'Hello, World!' to 'Hello, Universe!'.

const parser = require('php-parser');
const phpParser = new parser({ parser: { extractDoc: true } });
let ast = phpParser.parseCode('<?php echo "Hello, World!"; ?>');

function modifyEcho(node) {
  if (node.kind === 'echo') {
    node.arguments[0].value = 'Hello, Universe!';
  }
  for (let key in node) {
    if (node[key] && typeof node[key] === 'object') {
      modifyEcho(node[key]);
    }
  }
}

modifyEcho(ast);
console.log(JSON.stringify(ast, null, 2));

Other packages similar to php-parser

Keywords

FAQs

Package last updated on 30 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc