Skip to content

Commit e47e861

Browse files
committed
Merge pull request #6 from php-school/various-fixes
Various Improvements
2 parents 8369bd8 + 90308e1 commit e47e861

9 files changed

+465
-6
lines changed

src/Factory.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ class Factory
1717
*/
1818
public function __invoke()
1919
{
20+
$lexer = new Lexer([
21+
'usedAttributes' => [
22+
'comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos', 'startTokenPos', 'endTokenPos'
23+
]
24+
]);
25+
2026
$parserFactory = new ParserFactory;
2127
$color = new Color;
2228
$color->setForceStyle(true);
2329
return new SyntaxHighlighter(
24-
$parserFactory->create(ParserFactory::PREFER_PHP7),
30+
$parserFactory->create(ParserFactory::PREFER_PHP7, $lexer),
2531
new SyntaxHighlightPrinter(
2632
new SyntaxHighlighterConfig,
2733
new ColorsAdapter($color)

src/Lexer.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace PhpSchool\PSX;
4+
5+
use PhpParser\Lexer\Emulative;
6+
use PhpParser\Parser\Tokens;
7+
8+
/**
9+
* Class Lexer
10+
* @package PhpSchool\PSX
11+
* @author Aydin Hassan <[email protected]>
12+
*/
13+
class Lexer extends Emulative
14+
{
15+
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null)
16+
{
17+
$tokenId = parent::getNextToken($value, $startAttributes, $endAttributes);
18+
19+
if ($tokenId == Tokens::T_ARRAY) {
20+
$startAttributes['traditionalArray'] = true;
21+
}
22+
23+
if ($tokenId == Tokens::T_EXIT) {
24+
$startAttributes['isDie'] = strtolower($value) === 'die';
25+
}
26+
27+
return $tokenId;
28+
}
29+
}

src/SyntaxHighlightPrinter.php

+53-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public function __construct(
5353
*
5454
* @return string Pretty printed statements
5555
*/
56-
public function prettyPrintFile(array $stmts) {
56+
public function prettyPrintFile(array $stmts)
57+
{
5758
$p = rtrim($this->prettyPrint($stmts));
5859
$p = preg_replace('/^\?>\n?/', '', $p, -1, $count);
5960
$p = preg_replace('/<\?php$/', '', $p);
@@ -492,6 +493,57 @@ public function pExpr_New(Expr\New_ $node)
492493
);
493494
}
494495

496+
/**
497+
* @param Expr\Array_ $node
498+
* @return string
499+
*/
500+
public function pExpr_Array(Expr\Array_ $node)
501+
{
502+
if ($this->options['shortArraySyntax'] || !$node->hasAttribute('traditionalArray')) {
503+
return '[' . $this->pArrayList($node, $node->items) . ']';
504+
} else {
505+
return 'array(' . $this->pArrayList($node, $node->items) . ')';
506+
}
507+
}
508+
509+
/**
510+
* @param Expr\Array_ $parent
511+
* @param array $nodes
512+
* @return string
513+
*/
514+
public function pArrayList(Expr\Array_ $parent, array $nodes)
515+
{
516+
$lineNumbers = [$parent->getAttribute('startLine', null)];
517+
foreach ($nodes as $node) {
518+
$lineNumbers[] = $node->getAttribute('startLine', null);
519+
}
520+
521+
//all the same line
522+
if (count(array_unique($lineNumbers)) == 1) {
523+
return $this->pCommaSeparated($nodes);
524+
}
525+
526+
$output = "\n";
527+
foreach ($nodes as $key => $node) {
528+
$output .= sprintf(" %s,\n", $this->p($node));
529+
}
530+
return $output;
531+
}
532+
533+
/**
534+
* @param Expr\Exit_ $node
535+
* @return string
536+
*/
537+
public function pExpr_Exit(Expr\Exit_ $node)
538+
{
539+
$construct = 'exit';
540+
if ($node->getAttribute('isDie', false)) {
541+
$construct = 'die';
542+
}
543+
544+
return $construct . (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : '');
545+
}
546+
495547
/**
496548
* @param string $string
497549
* @param string $type

0 commit comments

Comments
 (0)