Function create_function() is deprecated in php7.2 - laravel

Getting
Function create_function() is deprecated Error in laravel 5.4 and php 7.2.
Not able to find solution.
please see below code
public static function delimiterToCamelCase($string, $delimiter = '[\-\_]')
{
// php doesn't garbage collect functions created by create_function()
// so use a static variable to avoid adding a new function to memory
// every time this function is called.
static $callback = null;
if ($callback === null) {
$callback = create_function('$matches', 'return strtoupper($matches[1]);');
}
return preg_replace_callback('/' . $delimiter . '(\w)/', $callback, $string);
}
please help.

create_function has been deprecated as of php 7.2. Instead you can use anonymous_function.
if ($callback === null) {
$callback = function ($matches) {
return strtoupper($matches[1]);
};
}

Related

Why squizlabs/php_codesniffer marked as error code with(new Vote)?

I laravel 9 project with squizlabs/php_codesniffer my phpstorm 2021 shows error :
Expected parameter of type '\TValue', 'Vote' provided
in model when I use table name in scope condition :
class QuizQualityResult extends Model
{
protected $table = 'quiz_quality_results';
public function scopeGetByVoteCategories($query, $voteCategoryId= null)
{
// “new Vote” is marked as error
$voteTable = with(new Vote)->getTable();
if (!empty($voteCategoryId)) {
if ( is_array($voteCategoryId) ) {
$query->whereIn( $voteTable . '.vote_category_id', $voteCategoryId);
} else {
$query->where( $voteTable . ' . vote_category_id', $voteCategoryId);
}
}
return $query;
}
If there is a way to fix this error ? Or maybe to use better syntax here ?
Thanks!
There is no need for helper with() here
$voteTable = (new Vote())->getTable()
Ps: there is a feeling that your method does not work the way you intended. Perhaps you meant to do the following (I could be wrong):
public function scopeGetByVoteCategories($query, $voteCategoryId = null)
{
if (empty($voteCategoryId)) {
return $query;
}
return $query->whereHas('vote', static function ($query) use ($voteCategoryId) {
if (is_array($voteCategoryId)) {
return $query->whereIn('vote_category_id', $voteCategoryId);
}
return $query->where('vote_category_id', $voteCategoryId);
});
}
public function vote()
{
// your relationship
}

Api Platform DTO object created by transformer is not getting persisted

Using Api Platform, I have a problem using an input class and its transformation.
The following documentation has been followed.
https://api-platform.com/docs/core/dto/#using-data-transfer-objects-dtos
After Data Transformer service executes transformation and returns an object of the correct class, the object that is picked up by api-platform appears to be empty, so it either fails validation, if validation is present, or persistence to the database fails - due its fields appear to be empty.
Here is a simplified code of DataTransformer service methods - it produces an object with hardcoded values:
public function transform($object, string $to, array $context = [])
{
$newCreativeElement = new CreativeElement();
$newCreativeElement->setKeyName("HARDCODED VALUE");
$newCreativeElement->setIntValue(42);
return $newCreativeElement;
}
public function supportsTransformation($object, string $to, array $context = []): bool
{
if ($object instanceof CreativeElement){
return false;
}
$result = CreativeElement::class === $to && null !== ($context['input']['class'] ?? null);
return $result;
}
Edit:
It's solved by 2.4 release.
Upgrade your composer.json and enjoy.
I have the same problem.
Something i tried is return for an array instead of an object transform method. It's working but not real solution.
it's appear that denormalizer is called 2 times : once for your transformer, and after to transform "CreativeElement" into "CreativeElement" by AbstractItemNormalizer
$context['api_denormalize'] = true;
$context['resource_class'] = $class;
$inputClass = $this->getInputClass($class, $context);
if (null !== $inputClass && null !== $dataTransformer = $this->getDataTransformer($data, $class, $context)) {
$data = $dataTransformer->transform(
parent::denormalize($data, $inputClass, $format, ['resource_class' => $inputClass] + $context),
$class,
$context
);
}
return parent::denormalize($data, $class, $format, $context);
Looking for solution too

Laravels object_get() helper broken in PHP7

We are working on an older Laravel 4.2 project and the new environment it is going on requires PHP7. We are trying to get this working, but we notice that the object_get() helper does not seem to work properly. Is there anything in this code that would not work from PHP 5.6 to 7?
function object_get($object, $key, $default = null)
{
if (is_null($key) || trim($key) == '') return $object;
foreach (explode('.', $key) as $segment)
{
if ( ! is_object($object) || ! isset($object->{$segment}))
{
return value($default);
}
$object = $object->{$segment};
}
return $object;
}

Missing argument 1 for Teepluss\Theme\Theme::Teepluss\Theme\{closure}()

i am using a theme manager for la-ravel Teepluss but when i bind any things it gives this error :
Missing argument 1 for Teepluss\Theme\Theme::Teepluss\Theme{closure}()
here is the screenshot of error
http://i.imgur.com/elQ5qLY.png
just adding this line $this->theme->bind('active', 'home'); to the below code gives error
public function showWelcome()
{
$this->theme->layout('default');
$this->theme->setTitle($this->config['SEO']['home']['title']);
$this->theme->setMeta_desc($this->config['SEO']['home']['meta_description']);
$this->theme->setMeta_keywords($this->config['SEO']['home']['meta_keywords']);
$this->theme->bind('active', 'home');
return $this->theme->scope('home.index')->render();
}
i think the package theme binding has some issues
Finally i found the issue. There is bug in this package. There shouldn't be any argument in the clouser and the code should be something like this :
public function bind($variable, $callback = null)
{
$name = 'bind.'.$variable;
// If callback pass, so put in a queue.
if ( ! empty($callback))
{
// Preparing callback in to queues.
$this->events->listen($name, function() use ($callback, $variable)
{
return ($callback instanceof Closure) ? $callback() : $callback;
});
}
// Passing variable to closure.
$_events =& $this->events;
$_bindings =& $this->bindings;
// Buffer processes to save request.
return array_get($this->bindings, $name, function() use (&$_events, &$_bindings, $name)
{
$response = current($_events->fire($name));
array_set($_bindings, $name, $response);
return $response;
});
}

CodeIgniter Controller Method Parameters Issue

I'm using codeigniter 2.1 and I defined a function as follows.
public function reset($email, $hash) {
}
According to MVC architecture and OOPS concept, the function could not execute if I did not pass the parameters in the url. But in codeigniter this function gets executing, So how can i overcome this?. Please help me to find solutions.
Just you need to define null parametra like this:
public function reset($email = null, $hash = null) {
}
If you call function
(controller name)/reset/mail#mail.com/dsadasda
than $email = mail#mail.com & $hash = dsadasda
if you function
(controller name)/reset
than $email and $hash will be null.
Also you can declare default parametre like this.
public function reset($email = mail#mail.com, $hash = dsadasdas) {
}
Hope that I was clear.
If you want to execute function with or without parameters
you can set default values for it.
public function reset($email = '', $hash = '') {
}
This way when there are no parameters function can still execute.
You can use condition for code
public function reset($email = '', $hash = '') {
if(!empty($email) AND !empty($hash)){
//your code here
}
}

Resources