Integrate PHP CodeSniffer with NetBeans IDE [Steps]

CodeSniffer, an essential development tool that ensures your code remains clean and consistent, should always be with you while writing code to check and fix the messy things. For those people who use NetBeans IDE to write PHP applications, here are the steps to integrate PHP CodeSniffer with NetBeans IDE.

Steps:
  • Open NetBeans IDE. ( Let me tell you, I am using NetBeans IDE 7.3 for this demo )
  • Install the NetBeans plugin ‘phpCS-MD’ by going to Tools -> Plugins screen.
    Click on the “Available Plugins” tab and search for ‘phpCS’ in the search box provided on top-right corner. Click and check the plugin and click on the “Install” button.
  • Once installed, lets configure it.
    Go to “Tools” -> “Options”.
  • Click on the tab “CodeSniffer”.
    Here you have to provide the path to phpcs.bat file present under your PHP installation directory. Do this by clicking the “Change” button there.
  • Click on the “Test Settings” button and if it shows the PHP CodeSniffer version details, then its configured successfully.

Now, click OK and go back to your code for checking against a particular coding standard. If you want to set a different coding standard definition, then setup that standard before hand on CodeSniffer. How to do that ? Follow my previous post to know how to install CodeSniffer and various Coding Standard definitions. Go to the CodeSniffer setting tab on NetBeans and set the Coding Standard definition name there in the input box named “Standard”.

On the code screen, you can see a new button with a single Blue Eye on the tool bar. This is to check any file for code issues. Click on that and you can see a new section comes below your code named “PHP CS/MD” and displays all the problems in the code row-wise. Now fix the messy code. Happy Coding πŸ™‚

Here are the step by step screen shots to make it easier to follow.
subharanjan_codesniffer_neatbeans_1

subharanjan_codesniffer_neatbeans_2

subharanjan_codesniffer_neatbeans_3

subharanjan_codesniffer_neatbeans_4

subharanjan_codesniffer_neatbeans_5

How to setup PHP CodeSniffer on Windows machine ?

How to use CodeSniffer on Windows ? How to setup the PHP CodeSniffer on Windows ? These questions come to mind when you see various articles, tutorials which provides ways/steps to setup this on Linux. So here are simple steps to setup PHP CodeSniffer on Windows. Before going into this, for those who don’t know what is CodeSniffer? here is a simple answer.

If you are a developer or code reviewer and you want to check the code for any mistakes/if the code following particular coding standards, then CodeSniffer is a good tool to achieve that.

PHP_CodeSniffer is a PHP5 script that tokenises PHP, JavaScript and CSS files to detect violations of a defined coding standard. It is an essential development tool that ensures your code remains clean and consistent. It can also help prevent some common semantic errors made by developers.

Now, lets setup CodeSniffer to check if our code follows a defined coding standard. πŸ™‚

My setup environment:

  • Windows 7 Professional,
  • XAMPP for Windows( Apache, MySQL, PHP and Perl ) and the PHP install directory is C:xamppphp
Steps:
  • Open windows command prompt by going.
    Start -> Run -> type in ‘ cmd ‘ -> press Enter
  • Go to your php install directory.
  • Then type in the command to install PHP Code_Sniffer from the pear.php.net
  • subharanjan_codesniffer_installation

  • After completion, to check successfull installation run this command.
    if everything goes well, then it should show list of coding standard definitions found.

As the CodeSniffer is installed now, you can start using this to check files by using below commands. ( To run any commands, you should be inside C:xamppphp )

Checking a single file or folder:

Sample PHP_CodeSniffer output with no warnings:

Specifying a coding standard to use:

For more usage commands: http://pear.php.net/manual/en/package.php.php-codesniffer.usage.php

How to add a new coding standard definition into PHP CodeSniffer ?

Lets take WordPress coding standard for demo purpose found here: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards

Steps:
– If you have git installed on you machine, then run this command on git bash.

subharanjan_codesniffer_git_bash
OR

If you don’t have git on your machine, then

subharanjan_codesniffer_directory_structure
Now you can check any file for WordPress coding standard.

How to reduce 404 errors created by ++liker.profile_URL++

Are you getting lots of 404 errors in the log because of some urls having patterns like ” ++liker.profile_URL++ ” ? How to reduce 404 errors created by ++liker.profile_URL++ ? Here is the solution to stop those 404 errors and convert those urls to result in 301 permanent redirects to your homepage.

Problem:
Some days back, when I saw the log created by Better WP Security plugin installed on my blog, there were a lot of 404 errors because of some weird URLs something like this:

  1. subharanjan.com/subharanjan.com/subharanjan.com/remove-or-override-functions-attached-to-a-specified-filter-hook/++liker.profile_URL++
  2. subharanjan.com/++liker.profile_URL++

I tried to find out about this and seems like this is a link dropping spambot. I am not sure what exactly it does but here is a solution to stop this.

Solution:

  • Open you .htaccess file.
  • Add these lines below into your .htaccess file which can be found at the root of your WordPress installation.
    Add these lines before the lines( url rewrites ) added by WordPress for permalink support. It should look like this:
  • Save the .htaccess file and check by accessing one of the urls having ++liker.profile_URL++ and see if it results in a 404 Page not found error OR simply redirects to your home page.

Explanation of the above htaccess code snippet:
RewriteCond will be looking for the pattern “++liker.profile_URL++” in the requested urls. Once it finds that piece of text in the requested url, it will redirect the request to home page in place of creating a 404 error as mentioned in the RewriteRule section.

RewriteRule defines a particular rule.
The first string of characters after RewriteRule defines what the original URL looks like.
The second string after RewriteRule defines the new URL. This is in relation to the document root (html) directory. / means the html directory itself, and subfolders can also be specified.

  • ^ begins the line to match.
  • . stands for “any non-whitespace character”
  • * means that the previous character can be matched zero or more times.
    So, ^uploads.*$ matches uploads2009, uploads2010, etc.
    ^.*$ means “match anything and everything.” This is useful if you don’t know what your users might type for the URL.
  • () designates which portion to preserve for use again in the $1 variable in the second string. This is useful for handling requests for particular files that should be the same in the old and new versions of the URL.
  • [NC] matches both upper- and lower-case versions of the URL.
  • [R=301,L] – this performs a 301 redirect and also stops any later rewrite rules from affecting this URL. It’s on the same line as RewriteRule, at the end.

Source: https://kb.mediatemple.net/questions/85/Using+.htaccess+rewrite+rules#gs

Refreshing new look of php.net

PHP.NET website has got a new refreshing look. Yes, with the recent launch of PHP 5.5.0 they have changed to the new layout for php.net It looks awesome !!

For the time-being, a new banner has been added to the top of the php.net website introducing everyone to the new theme. Go and have a look.. The new look of php.net is really nice πŸ™‚

php net new theme

Where to find the JSView Firefox Addon – Discontinued from Addons Repo.

Update: Date: 6th Sept. 2014

From Firefox 32(Version 32) onwards, this add-on JSView has stopped working and is no more compatible and won’t be in future versions of Firefox as well. However there is an alternative add-on which can be used for getting the list of JS and CSS file being loaded with web pages. Its the “Phoenix” add-on. πŸ™‚

Phoenix will tell you how many CSS and JS files are loaded into a page, how big these are, it will let you edit, pack and de-obfuscate them too.

Download the “Phoenix” add-on, the alternative for “JSView”. https://addons.mozilla.org/de/firefox/addon/phoenix/

Where can I find the popular JSView firefox addon which I have been using since a long time. Today I formatted my system and reinstalled firefox. JSView Suddenly I found that the the most useful firefox addon to list/view/copy path/url of JS files and CSS files is discontinued from the official firefox addon repository. On the mozdev.org site, its mentioned that ” JSView appears to have been abandoned by the author. It hasn’t been updated since July 2008 ”
However, we can still use this addon( of-course with personal choice/risk ) by taking it directly from the stored location as it is no more displayed on the addons/extensions list.

To install this addon on your firefox browser, open the link on firefox and follow the procedure.
http://downloads.mozdev.org/xsidebar/mods/jsview-2.0.8-mod.xpi

Sources:

SQL query to get post and its meta_values as one row of record (single data grid)

EAV to Single row of Record with Separte Column

How to query EAV attributes into separate columns ? Here is the SQL query to get post and its meta_values as one row of record (single data grid). πŸ™‚ πŸ™‚ WordPress stores the post data mainly in two tables (i.e. wp_posts and wp_postmeta). The first table stores the data which are common and apply to all the individual entities( like the entities of type post, page, media, attachment, custom post type etc..) Second table stores the attributes of those entities as key-value pair. So this is kind of following EAV model. For those who doesn’t know about EAV, it stands for Entity Attribute & Value. Generally when we are not following EAV style, we store the entity and its attributes as separate columns in one table, but in EAV each attribute is stored as rows. Usually it is done for a purpose: to allow columns to be added without having to make DDL changes . 6NF and EAV as principles and concepts offer substantial benefits, and performance increases.

But on certain scenarios we want the attributes stored as rows to be fetched and displayed as columns in single data grid. This is the most tedious part to select the data from two different tables and showing as single row of entity. However we can fetch and display the data by writing query somewhat like the following.SQL query to get post and its meta_values as one row of record.

This will display the posts which is having at least one of the above meta_keys set in the post_meta table. To display all the posts including those which doesn’t have any meta values in the post_meta tables.

EAV to Single row of Record with Separte Column

EAV to Single row of Record with Separte Column

Mashable changed its platform from WordPress to Ruby on Rails

Mashable New Look

Mashable New LookMashable, the latest in social media, tech, world news, business, entertainment related blog was based upon wordpress cms. Recently it has changed its look and feel with completely new style and fully responsive layout. Ruby on Rails with Backbone JS and Handlebarsare the platforms used to built the new site. Features like Adaptive Design, “Next Big Thing” and “Velocity” sections, Infinite scroll features inplace of old pagination and Microcontent facilty to share thing on the socual web have been added.

To see the design and layout of Mashable visit: Masahable

From Quora:
Read Quote of Chris Heald’s answer to Mashable: Is Mashable still using WordPress as the cms/platform ? What are the technologies behind this new look and feel? on Quora

Mashable is now running on a custom technology stack. The public-facing bits are a Ruby on Rails backend, with a lot of the templating work pushed to the front-end with Handlebars and Backbone. We’ll be doing a full writeup on the build in the near future.

http://qr.ae/pWOHf

Chromebook @ $249 from Google

The Samsung Chromebook is an ultraportable, sleek laptop for everyday adventures. It weighs less than 2.5 pounds and offers more than 6.5 hours of battery so you can take it wherever you go. Starting at $249
Hardware Details:

  • 11.6’’ (1366×768) display
  • 0.8 inches thin – 2.5 lbs / 1.1 kg
  • Over 6.5 hours of battery 1
  • Samsung Exynos 5 Dual Processor
  • 100 GB Google Drive Cloud Storage2 with Solid State Drive
  • Built-in dual band Wi-Fi 802.11 a/b/g/n
  • VGA Camera
  • 1x USB 3.0, 1x USB 2.0
  • HDMI Port
  • Bluetooth 3.0β„’ Compatible

Software Details:

  • OS: Chrome OS

Details with Specs & Gallery:

WordPress 3.5 (Beta 1) and Twenty Twelve theme is released

WordPress 3.5 Beta 1 is now available to download with lots of improvements. Another great news is the default theme which will be inside the WordPress 3.5 is also released and can be downloaded now separately for use. Have a look here: Twenty Twelve theme preview
Download it from WordPress.org


Loading facebook social plugin into AJAX generated dynamic content. [Solved]

To include Facebook social plugins(ex: Facebook Comments http://developers.facebook.com/docs/reference/plugins/comments/) into our site, we use some java-script and some HTML structure provided by facebook. It works fine until you have AJAXified your pages. If your site refreshes each page on each request then facebook social plugins work perfect.
Problem occurs when the HTML structure is loaded via AJAX and the script used for FB is not called again after the ajax response. This happens because Facebook parses the page only on initial load and so doesn’t pick up the dynamic content. To make it work on AJAXified content, we need to explicitly tell it to re-parse the entire page, or just the new content.

Recently, I used a plugin called “Ajaxify WordPress Site” which makes a wordpress website completly AJAXified semlessly. Really nice plugin to ajaxify WP sites without any hassel, but one problem I faced with this was Facebook Comments not being rendered after AJAX response for which the answer is “FB.XFBML.parse()” function call.

Example:

1. JavaScript SDK which needs to be included once on the page.

2. HTML code to be placed wherever we want facebook plugin to appear on your page.

So, in our case, say this bit of HTML code comes dynamically with AJAX not on page refresh. So how can we display the facebook comments ?

Answer is simple: Call the ” FB.XFBML.parse() ” explicitly inside the AJAX success function, which will re-parse the html and render the Facebook comments section. πŸ™‚

3. Code for AJAX success block with “FB.XFBML.parse()” call.

Hope this will help someone solving facebook social plugin issue with AJAX content.. πŸ™‚