How to replace phpdocs with new attributes in php 8 - phpdoc

I am using laravel. I need to know how phpdoc can be written in php 8 with attibutes.
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
//Some code
return [];
}
Can someone please explain how the above code can be written with attributes.

I believe you have misunderstood what Attributes are for, and how they related to doc blocks. There are two common uses of doc blocks (comments marked with /** ... */):
To document the code (hence the name "doc block"), in a mostly-standardised way which can be read by various tools, including documentation generators and IDEs.
To add machine-readable annotations to the code, for use with libraries and frameworks that can automatically generate behaviour based on those. For instance, an ORM might use an annotation of #TableName('Foo') to link a class to a particular database table, and generate appropriate SQL.
The example you've shown is of the first usage. What you have written is still the correct way of writing documentation.
PHP 8's native Attributes replace the second usage. It's up to the library what Attributes to look for, but an ORM that previously looked for #TableName('Foo') in a docblock might now look for #[TableName('Foo')] as a native Attribute instead.

There is a great automatic refractoring tool called rector. It will make your life so much easier. Install it, then create a rector.php file at the root directory that should look like something like this:
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Symfony\Set\SensiolabsSetList;
use Rector\Symfony\Set\SymfonySetList;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests'
]);
$rectorConfig->sets([
DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
SensiolabsSetList::FRAMEWORK_EXTRA_61,
]);
};
There are plenty of configuration options that you can find in the docs.
When you are done, just run the refractoring tool:
vendor/bin/rector process src

Related

Isolate external php code in Laravel

I need to integrate Slider Revolution editor into my Laravel (5.5) app.
I've put the editor in public/revslider/ folder to be able to use the visual editor. I also created a helper class to "communicate" with it and be able to use it inside my Blade views:
namespace App\Helpers;
include( public_path('revslider/embed.php') );
class Slider{
/**
* This function is called where you want to put your slider
*/
public static function make($slider){
return \RevSliderEmbedder::putRevSlider( $slider );
}
/**
* This function is called inside <HEAD> tag to include all
* SR assets (js/css/font files)
*/
public static function head(){
return \RevSliderEmbedder::headIncludes(false);
}
}
The SR's PHP code does not use namespaces. In fact it is a strange mix of Code Igniter, Wordpress and vanilla php.
The problem is it is trying to declare a translation function __(...):
if( ! function_exists('__'))
{
function __($string = '')
{
....
}
}
and since there is already such Laravel's helper function, it does not redeclare it and tries to use Laravel's __() function. And that obviously causes errors.
I temporarily managed to fix this problem by changing the name of SR's __() function (and all references to it). But of course it is not a best way to solve this problem, since I will be unable to use SR's automatic updates or will be forced to do these changes after every update.
So my questions are:
Is there any good way of integrating such "bad" code into your project, invoking it safely without conflicts? Is there any way of isolating such code and avoid clashes? By "bad code" I mean code that does not follow strict OOP/PSR rules present in projects like Laravel.
What is the best way to include "external" PHP code? I've just used plain include() inside of my helper class' file, but is there a better/cleaner way? Like, I don't know, loading it through composer?

How to change the language in Laravel Spark?

How can I change the language of Laravel Spark without editing all the blade-files? I could edit all blade-files and change the labbels/messages with the lang() function, but then I don't get the blade updates anymore (or I have to redo this after every update).
I have this very same problem, not only with translations, but also with small changes made to layout and stuff, and, unfortunately, I don't see anything else we, or they, could do now, for translations it could be fairily easy, but for other changes, not really. Changes are verified using MD5, so if you change a single letter in your view you make it unupdatable.
The day after installing it, and changing some views I was already stuck with Spark not being able to upgrade my views because of those changes, and I basically had to go thru the changed files and see if there was something important I would have to copy back to mines.
Thinking that they might at some point add new features to Spark, we would never get them automatically, if we change those views. That's why my decision was to touch only login and register, and let Spark deal with everything else in the Spark panel, while I build a completely separate system around it using my own template. At some point I know I'll have to add settings, so I'll also have to decide about having a second settings page, using my own template, or just edit Spark views and get back to this impossible-to-auto-upgrade state.
Spark is not yet prepared to be multi-language, but now, with Mohamed Said in Laravel Team, we should probably see some changes in this area.
To replace Spark views with yours, you just have to 'override' the spark:: namespace setting your own directory:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app['view']->addNamespace('spark', resource_path('/views/vendor/'.$namespace'));
}
}
This code adds a new folder to the list of ones Laravel Spark already has, in this case it will be the my resources/view.
You'll also have to move AppServiceProvider to a line above Laravel\Spark\Providers\SparkServiceProvider::class, in config/app.php.
And you don't have to have all Spark views in this folder, Laravel is kind of eager finding views, so if it doesn't find one, it will try to find it in the original spark folders.

What for is the direct_front_name tag in Magento

Some modules have a <request><direct_front_name>...</direct_front_name></request> in their module config, for example xmlconnect and api. What is this tag for?
What I think it is for:
xmlconnect and api are both used as direct entry points for the site (as opposed normal modules which are reached mostly from within the site). So in combination with the option to use store codes in your store urls you can specify a direct_front_end tag to make the store code not necessary for those modules. This way there is no 404 when calling them without a store code.
(Kind of answered it myself, but couldn't find any information about it online. Might be of use to others. And maybe anyone has something to add.)
You're totally right. And the php DOC clearly tells so :
Mage_Core_Controller_Request_Http::isDirectAccessFrontendName() :
/**
* Check if code declared as direct access frontend name
* this mean what this url can be used without store code
*
* #param string $code
* #return bool
*/
public function isDirectAccessFrontendName($code)

How to enable Auto Complete in PHPStorm for CodeIgniter framework

In CodeIgniter Project, I've normally use following commands to execute sql.
$res = $this->db->select('*')
->from('customer')
->where('customer.id', $id)
->get();
But unfortunatly my PHP Storm(5.0) didn't support multiple autocomplete(I don't know how to say this)
For example in netbeans If I typed
$res = $this->db->select('*')->
It will auto pop up the rest of the function. But In PHPStorm It didn't wokring.
Its working first level auto complete only.
download https://github.com/topdown/phpStorm-CC-Helpers/releases
Mark as Plain Text
/system/core/Controller.php
/system/core/Model.php
/system/database/DB_active_rec.php
Then Extract the downloaded archive, copy it to your project root
That's all
Mifas links do the same too though
Answering to a very old but still pertinent question -
I found a better solution herein - http://validwebs.com/346/code-completion-for-codeigniter-in-phpstorm/ and coincidentally it is from the same author/project owner Jeff Behnke.
Quoting from therein which should be read in continuation of the answer by Sabir -
Mark as Plain Text
/system/core/Controller.php
/system/core/Model.php
/system/database/DB_active_rec.php
Marking those files as plain text stops phpStorm from indexing them as
sources.
I consider the solution in the link better because it explains the rationale behind the steps performed.
It additionally explains how we can achieve code completion in views and fix for undefined vars.
Quoting once again from the original source for easy reference and preservation herein :
Code Completion in Views and fixing undefined vars.
Example controller code.
public function index()
{
// Example view vars
$data['test'] = 'Testing vars in CodeIgniter! This is from $data["test"].';
$this->load->view('welcome_message', $data);
}
We added a data array to the view the CI way. Each index in the array
is another variable.
The view…
In phpStorm $test will be highlighted as an undefined var. To fix this
we use phpDoc annotations.
<p style="font-weight: bold;">
<?php
/**
* $data array holds the $test value
*
* #see Welcome::index()
* #var Welcome $test
*/
echo $test;
?>
</p>
Documenting this way not only fixes the phpStorm error/warning but
also gives us documentation popup for $test. Also the #see will link
to the location it was created, in this case index method in the
Welcome class.
The var is now defined and shows it is.
Ctrl+ Click on this method link will bring you right to the method
where $test is defined.
Herein are a few discoveries of my own while adding customisations to my project:
If you want your custom application libraries from CI to be available for auto-completion as well then there are these 2 scenarios which may be helpful :
1. For custom extended libraries such as MY_Upload extending the CI_Upload class
Replace #property CI_Upload $upload with #property MY_Upload $upload in CI_phpstorm.php
This shall make all the class variable and function names of MY_Upload available for auto-completion in addition to that of CI_Upload.
2. For completely custom libraries written from scratch within the CI application -
For e.g. to enable auto-completion from Custom_Library.php residing in the application/libraries folder, you need to add to the php doc in CI_phpstorm.php #property Custom_Library $custom_library

Code completion for CodeIgniter with zend studio not working

Ok so i tried doing what this post says about adding code completion to zend studio, even with the libraries added code hinting/completion still fails to show. As my libraries are growing it's becoming more important to have this feature as i'm starting to forget what my methods were actually designed for or what there called. from within a controller, model maybe even view i would like to have it show after typing $this->router->(show completion) as an example. I have also added application/libraries path for my custom made libraries and they too wont show.
I have done a bit off google searching but most just say to do what the above post says. I attempted to try /* #var $var Class */ but wasnt able to assign it with a property eg. /* #var $this->router CI_Router */ only a standard variable..
This was solved simply by doing comment as such
/**
* #var CI_Loader
*/
$load
and not just doing
/* #var ........ */

Resources