Trailing comma in array causing Parse Error - php

I'm currently working on some project on Symfony 2.8.
I've come across this weird PHP error.
I'm building a symfony command that allows using Kahlan test framework inside a Symfony project. Thus, I need the Symfony container to be available in Kahlan test suites, and so to be defined as global variable of said suites. Simple matter with the Container aware command.
But here comes the glitch. When I try to call a service that uses Symfony's security.authorization_checker service, the cli returns this error:
PHP Parse error: syntax error, unexpected ',' in /tmp/kahlan/data/www/project/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authorization/ExpressionLanguageProvider.php on line 55
The code of said file:
[...]
public function getFunctions()
{
return array(
new ExpressionFunction('is_anonymous', function () {
return '$trust_resolver->isAnonymous($token)';
}, function (array $variables) {
return $variables['trust_resolver']->isAnonymous($variables['token']);
}),
new ExpressionFunction('is_authenticated', function () {
return '$token && !$trust_resolver->isAnonymous($token)';
}, function (array $variables) {
return $variables['token'] && !$variables['trust_resolver']->isAnonymous($variables['token']);
}),
new ExpressionFunction('is_fully_authenticated', function () {
return '$trust_resolver->isFullFledged($token)';
}, function (array $variables) {
return $variables['trust_resolver']->isFullFledged($variables['token']);
}),
new ExpressionFunction('is_remember_me', function () {
return '$trust_resolver->isRememberMe($token)';
}, function (array $variables) {
return $variables['trust_resolver']->isRememberMe($variables['token']);
}),
new ExpressionFunction('has_role', function ($role) {
return sprintf('in_array(%s, $roles)', $role);
}, function (array $variables, $role) {
return in_array($role, $variables['roles']);
}),
);
}
[...]
Line 55 is the line of the last array element, thus said comma is the trailing comma.
The error is thrown only in cli.
An of course, I have tried removing the comma, everything works then perfectly. But obviously, I can't change Symfony's built-in classes.
So, How come PHP throws Parse Errors for a trailing comma in an array only in cli mode, and do you guys have any idea on how to fix this?
Thanks.

As seen in the kahlan-bundle repo, The problem was not in my code but seemed to be with kahlan.
Issue answered by Kahlan dev here.
Upgrading to the new kahlan version simply resolved the problem.

Related

Laravel Mail:: inside function creates error

I am making my own package.
I simplified the code to explain the weird error.
I have a simple method inside a simple controller. First, i am sending an email (printing in laravel.log) and works fine.
public function signup(){
Mail::send('vendor.proto.register.emails.proto-register-new-account', [], function ($m) {
$m->from('noreply#test.com', 'Hello');
$m->to('sfs#dgf.c', 'pepe')->subject('Test. Congratulations, your account was created');
});
return view('view path');
}
But when i want to move the Mail:: to a private method like this.
public function signup(){
$this->sendNotification2();
return view('view path');
}
public function sendNotification2(){
Mail::send('vendor.proto.register.emails.proto-register-new-account', [], function ($m) {
$m->from('noreply#test.com', 'Hello');
$m->to('sfs#dgf.c', 'pepe')->subject('Test. Congratulations, your account was created');
});
}
It crash an print the error
FatalErrorException in ClassLoader.php line 373: Maximum function nesting level of '100' reached, aborting!
Is it a bug? or i am doing something wrong ?
Solution.
Googling i found this solution. Notice i am using laravel 5.2 and php 5.6.7
Add in bootstrap\autoload.php this code ini_set('xdebug.max_nesting_level', 200); and it fix the problem.

Fatal Error On Zend Project Live On Server Random

I have a Zend2 project running on my localhost with no problems. The app runs perfect. I Uploaded it to my server and now it gets a fatal error but not every time.
Sometimes it says this,
Fatal error: Class name must be a valid object or a string in /home/public_html/vendor/zendframework/zend-stdlib/src/ArrayObject.php on line 230
public function getIterator()
{
$class = $this->iteratorClass;
return new $class($this->storage); // line 230
}
And sometimes it says this,
File
/vendor/zendframework/zend-stdlib/src/ArrayObject.php:184
Message:
Passed variable is not an array or object, using empty array instead
Never both and sometimes it loads perfectly with no problems. The file it references is in the vendor path this is the link,
public function exchangeArray($data)
{
if (!is_array($data) && !is_object($data)) {
throw new Exception\InvalidArgumentException('Passed variable is not an array or object, using empty array instead');
} // Line 184
if (is_object($data) && ($data instanceof self || $data instanceof \ArrayObject)) {
$data = $data->getArrayCopy();
}
if (!is_array($data)) {
$data = (array) $data;
}
$storage = $this->storage;
$this->storage = $data;
return $storage;
}
Any ideas why this would happen on a live server with a zend site but not on a localhost?
I found this post on github which I think it related to ZFCUser
Git Hub Post
Someone in the comments says this,
This issue is caused by the layout.phtml when there is an error. The layout needs to render but it doesn't have $this->url
I have no clue what he is talking about. Is anyone able to shoot me in the right direction?

Fatal error: Class 'Zend_Log' not found

I'm getting the following error in magento administration
Fatal error: Class 'Zend_Log' not found in /home/website/public_html/app/code/community/Uni/Fileuploader/Block/Adminhtml/Fileuploader/Edit/Tab/Products.php on line 241
This is a community extension, which has been working fine on my website. The error makes no sense to me, because the line 241 contains just a closing "}" character.
class Uni_Fileuploader_Block_Adminhtml_Fileuploader_Edit_Tab_Products extends Mage_Adminhtml_Block_Widget_Grid {
...
...
...
public function getRowUrl() {
return '#';
}
public function getGridUrl() {
return $this->getUrl('*/*/productgrid', array('_current' => true));
}
protected function getFileuploaderData() {
return Mage::registry('fileuploader_data');
}
protected function _getSelectedProducts() {
$products = $this->getRequest()->getPost('selected_products');
if (is_null($products)) {
$products = explode(',', $this->getFileuploaderData()->getProductIds());
return (sizeof($products) > 0 ? $products : 0);
}
return $products;
}
} // line 241, where error occurs
I can post the rest of the code, if you need it.
I noticed that if I upgrade to PHP 5.4 version the error disappears, but since 5.4 version causes other errors on my website, I have to continue using 5.3.
Any ideas on how to solve this?
The problem could be the name of one of the methods in your custom class.
Take for example the method name is getData() ,
Try searching for generic method names in your script, such as getData, which might be reserved by some of Magento’s core classes. I figure that these methods have predefined functionality, which your module is missing support for, and Zend then tries to write an exception to Zend log.
Reference link: netismine
I got the same error when rewriting a payment method.
public function authorize($payment, $amount)
Solved rewriting exactly the same main method:
public function authorize(Varien_Object $payment, $amount)
Magento 1.9.1.0/PHP 5.5

PHP: Accessing Enclosed Scopes from Global Functions

I'm scratching my head at some PHP code. I can't tell if it logically works yet, as I'm quite new to PHP and it won't execute yet due to a syntax error: syntax error, unexpected 'use' (T_USE), expecting '{'
The syntax error is tripping up on a use, despite the PHP version fully supporting it, and the syntax (as far as I can tell) being correct.
The code checks which database implementation is specified, and it'll define the functions for whichever one it is. Because I can't define functions returned from other functions as global functions, I store it in them in variables, 'wrapping' access via global functions that capture the variables from their outer scopes, or at least they should do.
The two lines yielding errors have been marked:-
function query_escaper($element_escaper) {
return function($query, $values) use ($element_escaper) {
return join(array_map(
function($query_elem, $value) {
return isset($value)
? $query_elem . $element_escaper($value)
: $query_elem
;
},
explode('?', $query),
$values
));
};
}
function define_postgresql_functions() {
[...]
$escape_query = query_escaper('pg_escape_literal');
#
# XXX The error occurs here :-
#
function escape_query() use ($escape_query) {
return call_user_func_array($escape_query, func_get_args());
}
[...]
}
function define_mysql_functions() {
[...]
$escape_query = query_escaper(function($str) {
return use_database(function($database) use ($str) {
return $database->real_escape_string($str);
});
});
#
# XXX The same error also occurs here :-
#
function escape_query() use ($escape_query) {
return call_user_func_array($escape_query, func_get_args());
}
[...]
}
switch ($database_info['type']) {
case 'postgresql':
define_postgresql_functions();
break;
case 'mysql':
define_mysql_functions();
break;
default:
throw new DatabaseNotImplementedException();
}
I'm looking for two possible solutions: either being able to capture those variables from the outer scope, or another way of defining global functions as functions returned from other functions.
The pattern function\s+\w+ always declares a function in the global scope, meaning you have to use the global syntax. This means you can't name the function if you're creating a closure. So this is invalid:
function escape_query() use ($escape_query) {
But this would work fine:
$escape_function = function() use ($escape_query) {
There are a few different ways to accomplish what you want. eval comes to mind, as does using a static variable in the function, or the OOP solution.
I think the OOP solution is probably the easiest:
class Escaper {
public static $escape_function;
}
// in your function
Escaper::$escape_function = function() use ($escape_query) { //...
// later
Escaper::$escape_function('a','b','c');

Php closure function as parameter to preg_replace_callback results in Internal Server Error 500

I have a preg_replace_callback which takes a closure (anonymous) function as 2nd parameter and it works perfectly fine on local, but when I deploy it to live environment it results in error => Internal server error 500. When i remove the Closure it works.
$regExPattern = '/\<%(?<content>.*?)%\>/';
$template = preg_replace_callback($regExPattern, function ($matches) use ($dataItem) {
if(isset($dataItem[trim($matches['content'])])) {
return $dataItem[trim($matches['content'])];
}
else {
return '';
}
}, $template);
Any suggestions how can i work arround this problem. I need to use $dataItem inside my callback function and pass it to preg_replace_callback.
My development environment is code igniter.
Anonymous functions only work in PHP 5.3 and up. You could use create_function() instead:
$regExPattern = '/\<%(?<content>.*?)%\>/';
$template = preg_replace_callback($regExPattern, create_function(
'$matches'
, 'if(isset($dataItem[trim($matches[\'content\'])])) {
return $dataItem[trim($matches[\'content\'])];
}
else {
return "";
}'
)
);
Untested, of course.

Categories