You can use the awesome nikic / PHP-Parser to create a tool for converting selected PHP classes (those with @TypeScriptMe
string to phpDoc) to TypeScript quite easily. The following script is really simple, but I think you can extend it, and you can automatically generate TypeScript interfaces and maybe track changes through git.
Example
For this input:
<?php
class Person
{
public $name;
public $age;
public $mixed;
private $propertyIsPrivateItWontShow;
}
class IgnoreMe {
public function test() {
}
}
:
interface Person {
name: string,
age: number,
mixed: any
}
index.php:
<?php
namespace TypeScript {
class Property_
{
/** @var string */
public $name;
public $type;
public function __construct($name, $type = "any")
{
$this->name = $name;
$this->type = $type;
}
public function __toString()
{
return "{$this->name}: {$this->type}";
}
}
class Interface_
{
public $name;
public $properties = [];
public function __construct($name)
{
$this->name = $name;
}
public function __toString()
{
$result = "interface {$this->name} {\n";
$result .= implode(",\n", array_map(function ($p) { return " " . (string)$p;}, $this->properties));
$result .= "\n}";
return $result;
}
}
}
namespace MyParser {
ini_set('display_errors', 1);
require __DIR__ . "/vendor/autoload.php";
use PhpParser;
use PhpParser\Node;
use TypeScript;
class Visitor extends PhpParser\NodeVisitorAbstract
{
private $isActive = false;
private $output = [];
private $currentInterface;
public function enterNode(Node $node)
{
if ($node instanceof PhpParser\Node\Stmt\Class_) {
$class = $node;
if ($class->getDocComment() && strpos($class->getDocComment()->getText(), "@TypeScriptMe") !== false) {
$this->isActive = true;
$this->output[] = $this->currentInterface = new TypeScript\Interface_($class->name);
}
}
if ($this->isActive) {
if ($node instanceof PhpParser\Node\Stmt\Property) {
$property = $node;
if ($property->isPublic()) {
$type = $this->parsePhpDocForProperty($property->getDocComment());
$this->currentInterface->properties[] = new TypeScript\Property_($property->props[0]->name, $type);
}
}
}
}
public function leaveNode(Node $node)
{
if ($node instanceof PhpParser\Node\Stmt\Class_) {
$this->isActive = false;
}
}
private function parsePhpDocForProperty($phpDoc)
{
$result = "any";
if ($phpDoc !== null) {
if (preg_match('/@var[ \t]+([a-z0-9]+)/i', $phpDoc->getText(), $matches)) {
$t = trim(strtolower($matches[1]));
if ($t === "int") {
$result = "number";
}
elseif ($t === "string") {
$result = "string";
}
}
}
return $result;
}
public function getOutput()
{
return implode("\n\n", array_map(function ($i) { return (string)$i;}, $this->output));
}
}
$parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative);
$traverser = new PhpParser\NodeTraverser;
$visitor = new Visitor;
$traverser->addVisitor($visitor);
try {
$code = <<<'EOD'
<?php
/**
* @TypeScriptMe
*/
class Person
{
/**
* @var string
*/
public $name;
/**
* @var int
*/
public $age;
/**
* @var \stdClass
*/
public $mixed;
/**
* @var string
*/
private $propertyIsPrivateItWontShow;
}
class IgnoreMe {
public function test() {
}
}
EOD;
$stmts = $parser->parse($code);
$stmts = $traverser->traverse($stmts);
echo "<pre><code>" . $visitor->getOutput() . "</code></pre>";
} catch (PhpParser\Error $e) {
echo 'Parse Error: ', $e->getMessage();
}
}
composer.json
{
"name": "experiment/experiment",
"description": "...",
"homepage": "http://example.com",
"type": "project",
"license": ["Unlicense"],
"authors": [
{
"name": "MrX",
"homepage": "http://example.com"
}
],
"require": {
"php": ">= 5.4.0",
"nikic/php-parser": "^1.4"
},
"minimum-stability": "stable"
}