Coding Standard tools

I dont think that now exists a file to do coding standard for Yii. I hope in Yii2.0 exists.

Now I am creating a project on github. I want to write in yii coding standard. But … I have to define all yii standard by hand!!! I dont know if you use PHP_CodeSniffer. It can control al your code and tell you if your application is written in a specific standard. Symfony2 and Zend has this feature. Yii has not?

As I remember from past discussions there is no strict coding standard for yii so it would not be possible to run a PHP_CodeSniffer on the code since not everything is defined in detail.

Well, we’re sticking to some kind of silent agreement on coding standard. At least for core so it’s possible even for Yii 1.1. The bad part is that you have to write definition file yourself. I can help answering questions about how Yii core is formatted.

Ok, someone want help me to write yii coding standard?

Does these "coding standards" include number of spaces inside different expressions? Yii developers prefer "spaceless" (sorry, dunno how to call it right) style:




public function save($runValidation=true,$attributes=null)

{

    if(!$runValidation || $this->validate($attributes))

        return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes);

    else

        return false;

}



I would write (and I think many people too):




public function save($runValidation = true, $attributes = null)

{

    if (!$runValidation || $this->validate($attributes))

        return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes);

    else

        return false;

}



always braces




public function save($runValidation = true, $attributes = null)

{

    if (!$runValidation || $this->validate($attributes)) {

        return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes);

    } else {

        return false;

    }

}



I dont care about braces. I care about standard. Any standard. But standard.

This is religious - and we had the discussion before - with a locked topic as the final result.

No, the team is not willing to write a coding standard. That was the very clear message we got back then.

However, I prefer spaces between operators, like Andy_s, and braces - even for a line line code block - as Mbi.

I also prefer 4 spaces instead of a tab (spaces instead of tabs).

That brings me to a feature request:

Could the Gii tool have a configuration option for tabs versus spaces, please?

Basically, what to insert: a tab (\t) or 2-4 spaces.

My editor of choice would be grateful.

That goes for the migration tool as well.

A configurable option would be great.

IMO that feature is better left to the editor… all modern editors has the ability to convert tabs to spaces and vice-versa.

I agree.

Still I prefer

  • no braces for single line statements (braces exist in order to group multiple lines, no multiple lines no braces)

  • space after conditions (they are not functions like f(x) )

;D

Looks like this topic is turning into the same direction as the one jacomoe mentioned.

That’s what I have to do now: each time I use the migration tool or the gii generator I have to convert it.

A configurable option would be far better.

Could default to a tab character or 4 spaces, as long as I can configure it. :)

Yes, I think that’s possible. Since we don’t care about Gii performance much, I think there’s no problem making it feature rich and configurable. Once Yii2 code will be at GitHub you’ll have an opportunity to help us implement all these useful features.

Er… doesn’t gii already feature templates? What’s wrong with modifying those?

Nothing wrong with it. Maybe it’s a better way actually.

[OT]

I was planning to have a repository of user-submitted templates for giix. Had no time to work on it yet.

Combining this (or the new approach for this in Gii) with Yii 2.0 packages can be very useful.

I agree to, if it’s not necessary to fix everythings, but just some rule to make the code more readable. If in 2 or 3 year I should get an old application with no rule, I whant not must be say; “put your work at trash and restart on another framework”.

In same time if i get the same project but from Symfony2 and is writted on the same way, I can’t say same and that just because Symfony2 have set writting rules and if the developper don’t follow and if the code isn’t readable, that mean the project manager is null and I put him in trash.

example of few rule:

  • no braces for single line statements (braces exist in order to group multiple lines, no multiple lines no braces)

  • space after conditions (they are not functions like f(x) )

  • private menber start by _ and not static member start in minus

  • indentation (space or tab, not problem but indent)

  • presence of doc generated automaticly (@param and @return minimum)

and that all (and i think that the minimum)

I think it would be very helpful to have an official code layout definition file (or what it is called) that you run through a tool.

As long as the def file is approved (and maintained by) the Yii team, it would simply remove any further need to discuss braces, spaces and whatever.

Just run the tool, and that’s it. :)