Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP syntax check #182

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,5 @@ COM_JEDCHECKER_LEVEL_COMPATIBILITY="COMPATIBILITY"
COM_JEDCHECKER_LEVEL_NOTICE="NOTICE"
COM_JEDCHECKER_LEVEL_INFO="INFO"
COM_JEDCHECKER_LEVEL_PASSED="PASSED"
COM_JEDCHECKER_PHP_SYNTAX_CHECK="Syntax check"
COM_JEDCHECKER_PHP_SYNTAX_CHECK_DESC="Warns about any syntax errors in your PHP files"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"config": {
"platform-check": false
},
"require": {
"nikic/php-parser": "^4.13"
}
}
75 changes: 75 additions & 0 deletions administrator/components/com_jedchecker/libraries/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* @package Joomla.JEDChecker
*
* @copyright Copyright (C) 2022 Open Source Matters, Inc. All rights reserved.
*
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die('Restricted access');

use PhpParser\ParserFactory;
use PhpParser\Error;


// Include the rule base class
require_once JPATH_COMPONENT_ADMINISTRATOR . '/models/rule.php';


/**
* class JedcheckerRulesPHPSyntaxCheck
*
* This class TODO
*
* @since 2.3
*/
class JedcheckerRulesPHPSyntaxCheck extends JEDcheckerRule
{
/**
* The formal ID of this rule. For example: SE1.
*
* @var string
*/
protected $id = 'PHPSYNTAXCHECK';

/**
* The title or caption of this rule.
*
* @var string
*/
protected $title = 'COM_JEDCHECKER_PHP_SYNTAX_CHECK';

/**
* The description of this rule.
*
* @var string
*/
protected $description = 'COM_JEDCHECKER_PHP_SYNTAX_CHECK_DESC';

/**
* The ordering value to sort rules in the menu.
*
* @var integer
*/
public static $ordering = 450;

/**
* Manifest's directory
*
* @var string
*/
protected $basedir;

/**
* PHP Parser
*
* @var \PhpParser\Parser
*/
protected $parser;

/**
* Initiates the search and check
*
* @return void
*/
public function check()
{
include_once JPATH_COMPONENT_ADMINISTRATOR . '/libraries/vendor/autoload.php';
$this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);

$files = JFolder::files($this->basedir, '\.php$', true, true);
foreach ($files as $file)
{
$this->find($file);
}
}

/**
* Reads a file and check syntax
*
* @param string $file - The path to the file
*
* @return boolean True if the check has been passed.
*/
protected function find($file)
{
$code = file_get_contents($file);

try {
$ast = $this->parser->parse($code);
} catch (Error $error) {
$this->report->addError($file, $error->getMessage());
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitbddd5bb9017c3732135dac7a0dcd5b1d::getLoader();
117 changes: 117 additions & 0 deletions administrator/components/com_jedchecker/libraries/vendor/bin/php-parse
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env php
<?php

/**
* Proxy PHP file generated by Composer
*
* This file includes the referenced bin path (../nikic/php-parser/bin/php-parse)
* using a stream wrapper to prevent the shebang from being output on PHP<8
*
* @generated
*/

namespace Composer;

$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';

if (PHP_VERSION_ID < 80000) {
if (!class_exists('Composer\BinProxyWrapper')) {
/**
* @internal
*/
final class BinProxyWrapper
{
private $handle;
private $position;
private $realpath;

public function stream_open($path, $mode, $options, &$opened_path)
{
// get rid of phpvfscomposer:// prefix for __FILE__ & __DIR__ resolution
$opened_path = substr($path, 17);
$this->realpath = realpath($opened_path) ?: $opened_path;
$opened_path = $this->realpath;
$this->handle = fopen($this->realpath, $mode);
$this->position = 0;

return (bool) $this->handle;
}

public function stream_read($count)
{
$data = fread($this->handle, $count);

if ($this->position === 0) {
$data = preg_replace('{^#!.*\r?\n}', '', $data);
}

$this->position += strlen($data);

return $data;
}

public function stream_cast($castAs)
{
return $this->handle;
}

public function stream_close()
{
fclose($this->handle);
}

public function stream_lock($operation)
{
return $operation ? flock($this->handle, $operation) : true;
}

public function stream_seek($offset, $whence)
{
if (0 === fseek($this->handle, $offset, $whence)) {
$this->position = ftell($this->handle);
return true;
}

return false;
}

public function stream_tell()
{
return $this->position;
}

public function stream_eof()
{
return feof($this->handle);
}

public function stream_stat()
{
return array();
}

public function stream_set_option($option, $arg1, $arg2)
{
return true;
}

public function url_stat($path, $flags)
{
$path = substr($path, 17);
if (file_exists($path)) {
return stat($path);
}

return false;
}
}
}

if (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) {
include("phpvfscomposer://" . __DIR__ . '/..'.'/nikic/php-parser/bin/php-parse');
exit(0);
}
}

include __DIR__ . '/..'.'/nikic/php-parser/bin/php-parse';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@ECHO OFF
setlocal DISABLEDELAYEDEXPANSION
SET BIN_TARGET=%~dp0/php-parse
SET COMPOSER_RUNTIME_BIN_DIR=%~dp0
php "%BIN_TARGET%" %*
Loading