Vy Do
Vy Do

Reputation: 52488

How to set Eclipse PDT follow Zend Framework 2 coding convention?

I am learning Zend Framework 2. When I press Ctrl + Shift + F combine keys for auto format code, it show doesn't like Zend Framework 2 coding convention.

Please show me an easy way to auto format code follow Zend Framework coding convention in Eclipse 4.4 Luna PDT (PHP Development Tool).

Upvotes: 4

Views: 1777

Answers (3)

Suf_Malek
Suf_Malek

Reputation: 666

Go TO Window -> Preferences -> PHP -> Code Style -> Formatter -> Select from active profile (Zend).

Upvotes: 1

edigu
edigu

Reputation: 10089

Zend Framework 2 coding standards are principally based on PSR-2 with some minor additions. I'm not really sure about that additions but it should be minimal and you can try to find by reading all of this and this. Also there is a meeting log exists from June 2012 which contains a discussion about this subject.

I never understand why they prefer to introduce yet another new coding standard named "zend" instead of simply following and supporting the PSR-2 as is.

Basically, i think adjusting your IDE for PSR-2 will be enough.

Here is some resources:

There is no easy & magic way exists to format your code based on ZF2 standards by using a keyboard shortcut combination but yes, it is possible. Every IDE has different characteristics, settings, best-practices and approaches.

All you need is adjusting your IDE carefully for PSR-2 after reading some documentation. I strongly recommend to use php-cs-fixer.

Hope it helps.

Upvotes: 1

S.Gartmeier
S.Gartmeier

Reputation: 491

I used to work with Eclipse and PDT a while. Then I switch to an other IDE. Anyway, in Eclipse you can edit how the autoformat will work. You can edit the Autoformat at Window -> Preferences and then under PHP -> Code Style. For me it worked well from the beginning with Zend Framework 2.

There you will also find the possibility to export / import settings. I just exported mine. You can save following code as .xml and then import it at the same location you are able to edit the settings. Hope this may will help you.

Don't forget, first make a backup of yours, just to be sure. (I used Eclipse 4.2.2 & PDT 3.1.1)

XML for Code Templates

<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="php_gettercomment_context" deleted="false" description="Comment for getter methods" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.gettercomment" name="gettercomment">/**
 * @return the ${bare_field_name}
 */</template><template autoinsert="true" context="php_settercomment_context" deleted="false" description="Comment for setter methods" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.settercomment" name="settercomment">/**
 * @param ${field_type} ${bare_field_name}
 */</template><template autoinsert="true" context="php_constructorcomment_context" deleted="false" description="Comment for created constructors" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.constructorcomment" name="constructorcomment">/**
 * ${tags}
 */</template><template autoinsert="true" context="php_filecomment_context" deleted="false" description="Comment for created PHP files" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.filecomment" name="filecomment">/**
 * 
 */
</template><template autoinsert="true" context="php_typecomment_context" deleted="false" description="Comment for created types" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.typecomment" name="typecomment">/**
 * @author ${user}
 *
 * ${tags}
 */</template><template autoinsert="true" context="php_fieldcomment_context" deleted="false" description="Comment for fields" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.fieldcomment" name="fieldcomment">/**
 * @var ${field_type}
 */</template><template autoinsert="true" context="php_methodcomment_context" deleted="false" description="Comment for non-overriding methods" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.methodcomment" name="methodcomment">/**
 * ${tags}
 */</template><template autoinsert="true" context="php_overridecomment_context" deleted="false" description="Comment for overriding methods" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.overridecomment" name="overridecomment">/* (non-PHPdoc)
 * ${see_to_overridden}
 */</template><template autoinsert="true" context="php_delegatecomment_context" deleted="false" description="Comment for delegate methods" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.delegatecomment" name="delegatecomment">/**
 * ${tags}
 * ${see_to_target}
 */</template><template autoinsert="true" context="php_newtype_context" deleted="false" description="Newly created files" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.newtype" name="newtype">${filecomment}
${package_declaration}

${typecomment}
${type_declaration}</template><template autoinsert="false" context="php_classbody_context" deleted="false" description="Code in new class type bodies" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.classbody" name="classbody">
</template><template autoinsert="false" context="php_interfacebody_context" deleted="false" description="Code in new interface type bodies" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.interfacebody" name="interfacebody">
</template><template autoinsert="true" context="php_catchblock_context" deleted="false" description="Code in new catch blocks" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.catchblock" name="catchblock">// ${TODO} Auto-generated catch block
${exception_var}.printStackTrace();</template><template autoinsert="true" context="php_methodbody_context" deleted="false" description="Code in created method stubs" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.methodbody" name="methodbody">// ${TODO} Auto-generated method stub
${body_statement}</template><template autoinsert="true" context="php_constructorbody_context" deleted="false" description="Code in created constructor stubs" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.constructorbody" name="constructorbody">${body_statement}
// ${TODO} Auto-generated constructor stub</template><template autoinsert="true" context="php_getterbody_context" deleted="false" description="Code in created getters" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.getterbody" name="getterbody">return ${field};</template><template autoinsert="true" context="php_setterbody_context" deleted="false" description="Code in created setters" enabled="true" id="org.eclipse.php.ui.editor.templates.php.codetemplates.setterbody" name="setterbody">${field} = ${param};</template><template autoinsert="true" context="php_new_file_context" deleted="false" description="Simple php file" enabled="true" id="org.eclipse.php.ui.editor.templates.php.author" name="New simple PHP file">&lt;?php
${cursor}</template><template autoinsert="true" context="php_new_file_context" deleted="false" description="html 4.01 frameset" enabled="true" id="org.eclipse.php.ui.editor.templates.php.html.frameset" name="New PHP file - HTML frameset">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=${encoding}"&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;frameset&gt;
    &lt;frame&gt;
    &lt;frame&gt;
    &lt;noframes&gt;
    &lt;body&gt;
    &lt;p&gt;This page uses frames. The current browser you are using does not support frames.&lt;/p&gt;
    &lt;?php
${cursor}
    ?&gt;
    &lt;/body&gt;
    &lt;/noframes&gt;
&lt;/frameset&gt;
&lt;/html&gt;</template></templates>

Formatter settings

The Settings from the menupoint Formatters (just below Code Templates) are set up like this in my Eclipse:

Tab Policy: Tabs

Indentation size: 1

Tab size: 4

Default indentatino for wrappen lines: 1

Default indentation for array initializers: 1

Upvotes: 1

Related Questions