News Feed
Sections




News Archive
feed this:

Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

NetTuts.com:
Refactoring Legacy Code Part 6 - Attacking Complex Methods
June 27, 2014 @ 13:17:37

The NetTuts.com site has posted the sixth part in their "Refactoring Legacy Code" series, this time with a focus on the more complex methods. They look at simplifying their contents and testing their various parts (better code coverage). The post is based completely on the contents of the previous five in the series, so if you haven't read up on those do that before starting.

In our previous five lessons we invested quite a lot of time in understanding our legacy system, in writing tests for whatever testable piece of code we could find. We reached a point to where we have quite a few tested methods but we still avoided the complex, hard to understand logic. It's now time for some serious coding.

The start with one of the more complex methods (roll) and work through it line-by-line to figure out what it's being given, how it's handling the data and what kinds of things it might return or modify inside. The break it down into to "parts" and figure out the right tests to write for each. With the method fully tested, they then start in on the refactor, teasing out various parts of the method into other methods and property changes. There's also a section at the end talking about pair programming and how it relates to good testing practices.

0 comments voice your opinion now!
refactor series tutorial part6 complex method unittest phpunit

Link: http://code.tutsplus.com/tutorials/refactoring-legacy-code-part-6-attacking-complex-methods--cms-21522

Mathias Verraes:
Named Constructors in PHP
June 13, 2014 @ 09:42:15

Mathias Verras has a new post to his site about an idea he calls "named constructors". This method uses static factory methods to simulate the idea of a constructor and initialize the object.

PHP allows only a single constructor per class. That's rather annoying. We'll probably never have proper constructor overloading in PHP, but we can at least enjoy some of the benefits. Let's take a simple Time value object. Which is the best way of instantiating it? The only correct answer is "it depends".

His example shows the typical constructor creation with variable arguments, but points out that this can get messy quickly. His other method, the factory methods as "constructors", can make for a cleaner interface and makes the class more flexible. They make the object able to be initialized with different types of values and even satisfies the Single Responsibility Principle. He goes through a few examples using his "Time" class, showing how different "constructor" methods can be used to handle inputs ranging from a normal hour/minute format out to a "from minutes since midnight" value.

0 comments voice your opinion now!
named constructor factory method static tutorial time

Link: http://verraes.net/2014/06/named-constructors-in-php/

NetTuts.com:
Refactoring Legacy Code Part 5 - Game's Testable Methods
May 27, 2014 @ 09:29:33

NetTuts.com continues on with their next part of their refactoring series today in this new post focusing on (unit) testing more of the application. This includes both the code for the tests and the before/after of the refactored code.

Old code. Ugly code. Complicated code. Spaghetti code. Gibberish nonsense. In two words, Legacy Code. This is a series that will help you work and deal with it. In our previous tutorial, we tested our Runner functions. In this lesson, it is time to continue where we left off by testing our Game class. [...] It is much better to start testing it by its short, testable methods. This is what we'll do in this lesson: find and test those methods.

They start with creating a new "Game" object and finding the first testable method in the class. The tutorial works through this and other related methods to build up a set of "Game" tests and eventually doing some refactoring on the tests themselves. With one method down and tested, they move on to finding and creating the tests for the next few testable methods, looking for something "controllable" that makes for easy testing.

0 comments voice your opinion now!
refactor legacy code series part5 unittest game method

Link: http://code.tutsplus.com/tutorials/refactoring-legacy-code-part-5-games-testable-methods--cms-21213

Community News:
PHPUnit Announced End of Life on PEAR Installation Method
April 21, 2014 @ 10:29:53

There's a new addition to the GitHub wiki that's quite important for the PHPUnit users out there. Sebastian Bergmann has officially announced the end of life for the PEAR version of the installer for the popular PHPUnit tool.

Since PHPUnit 3.7, released in the fall of 2012, using the PEAR Installer was no longer the only installation method for PHPUnit. Today most users of PHPUnit prefer to use a PHP Archive (PHAR) of PHPUnit or Composer to download and install PHPUnit. Starting with PHPUnit 4.0 the PEAR package of PHPUnit was merely a distribution mechanism for the PHP Archive (PHAR) and many of PHPUnit's dependencies were no longer released as PEAR packages. Furthermore, the PEAR installation method has been removed from the documentation. We are taking the next step in retiring the PEAR installation method with today's release of PHPUnit 3.7.35 and PHPUnit 4.0.17.

Included in this end of life, they'll also be decommissioning pear.phpunit.de to happen no later than the end of 2014.

0 comments voice your opinion now!
pear phpunit install method composer phar download

Link: https://github.com/sebastianbergmann/phpunit/wiki/End-of-Life-for-PEAR-Installation-Method

Nikita Popov:
Methods on primitive types in PHP
March 17, 2014 @ 12:11:22

In his latest post Nikita Popov highlights one of the topics from this post, primitive types as objects, and some alternative options.

A few days ago Anthony Ferrara wrote down some thoughts on the future of PHP. I concur with most of his opinions, but not all of them. In this post I'll focus on one particular aspect: Turning primitive types like strings or arrays into "pseudo-objects" by allowing to perform method calls on them. [...] Note that this isn't far off dreaming, but something that already exists right now. The scalar objects PHP extension allows you to define methods for the primitive PHP types. The introduction of method-call support for primitive types comes with a number of advantages.

Among the advantages he lists:

  • The opportunity for a cleaner API (instead of the current, sometimes oddly named functions)
  • Improved readability
  • Polymorphism through a "cleaning up" of shared methods
  • Loose Typing

He also looks at possible ways that other primitive types could be handled (like "null" or "float") and some of the problems that could come up when passing objects around. Since the values could be an object or scalar, how would you know the difference. He finishes off the post with a look at the current state of things, including that there's not much resistance just that there hasn't been a good API defined to make it work.

0 comments voice your opinion now!
method primitive type object example problems

Link: http://nikic.github.io/2014/03/14/Methods-on-primitive-types-in-PHP.html

MaltBlue.com:
Do We Use Magic Methods or Not?
December 13, 2013 @ 10:39:20

In the latest post to his MaltBlue.com site Matthew Setter takes a look at magic methods. He tries to answer a few basic questions about them - are they worth using and can you truly test effectively when they're in use.

As a freelance Zend Framework developer, I'm always looking to improve the quality of the applications I produce. So over the last 6 - 12 months, I've been learning as much as possible about testing. During this time, I've found the way I code's dramatically changing (and improving). [...] In a recent development session, I attempted to test some of my ZendDb based classes, specifically the code which used the magic methods for dynamically building where clauses. [...] I can't speak for what it's like using PHPUnit's mock objects, as I always use Mockery instead. But after attempting to do so in Mockery, I hit a stumbling block when trying to test the chained call.

His example is a call to "lessThanOrEqualTo" to create his where clause that makes use of the "__get" magic method to get and return "Where" object. After some research (and conversations on IRC) he started wondering if the magic methods were worth the trouble they may cause during testing. He references this post and lists several of the comments made about their use, most of them not in favor.

0 comments voice your opinion now!
magic method zendframework sql builder query unittest testing

Link: http://www.maltblue.com/php/php-magic-methods-or-not

Brandon Savage:
The Cardinal Sin Of Object Inheritance
September 09, 2013 @ 12:38:04

Brandon Savage talks about the "cardinal sin" of working with object inheritance in PHP applications - adding public methods to a class that extends/implements another.

I know I've committed this sin, and you probably have too. The sin of which I speak is a grave one, and it violates several well known and established principles of object oriented application development. What is this sin of which I speak? It is none other than the addition of new public methods to an object that extends or implements abstract class or application interface, in violation of both the Liskov Substitution Principle and the Dependency Inversion Principle.

He talks some about the Liskov Substitution Principle first, pointing out that adding those new methods makes the new object non-replaceable as the Liskov principle requires. As far as the Dependency Inversion Principle, the practice breaks it because you'd be depending on those new methods as concrete, not abstracted from the parent. He makes a few recommendations as far as ways to prevent violating these principles including using multiple interfaces or creating multiple abstract classes for different public APIs.

0 comments voice your opinion now!
object inheritance sin solid principle public method violation

Link: http://www.brandonsavage.net/the-cardinal-sin-of-object-inheritance/

Marco Pivetta:
Accessing private PHP class members without reflection
August 15, 2013 @ 12:53:55

Marco Pivetta has posted about an interesting trick you can do with closures in PHP related to accessing private properties inside classes.

A couple of weeks ago I was working on a very tricky issue on ProxyManager. The problem is simple: instantiating ReflectionClass or ReflectionProperty is slow, and by slow, I mean really slow! The reason for this research is that I'm trying to optimize a "hydrator" to work with larger data-sets by still keeping a low initialization overhead. PHP 5.4 comes with a new API for Closures, which is Closure#bind(). Closure#bind() basically allows you to get an instance of a closure with the scope of a given object or class. Neat! That's basically like adding APIs to existing objects! Let's break some OOP encapsulation to fit our needs.

He shows how to use this "bind" feature to reach into an object, in this case a "Kitchen", and extract the value of an internal, private property. He also talks some about the performance of this method versus the more typical use of Reflection. He includes two other quick examples too - accessing the same private properties by reference and an abstracted "property reader" closure that uses the bind trick on any object.

0 comments voice your opinion now!
private method reflection closure bind alternative performance

Link: http://ocramius.github.io/blog/accessing-private-php-class-members-without-reflection

James Morris:
PHPUnit Mocking and Method Chaining
July 17, 2013 @ 12:13:02

James Morris has an interesting new post about mocking and method chaining and a discovery he made about the proper use of the "at" method in what his mock objects were expecting.

I've been given the task of unit testing Symfony2's security layer, which at first seems daunting, but in reality with a clever bit of PHPUnit mocking, it's actually quite simple. Symfony2 makes heavy use of method chaining.

He illustrates one way to create the mocks for this chain (one mock returning another) but suggests an alternative - returning an instance of "self" to keep the chain alive. He also includes a bit about the "at" matcher and how, despite what the PHPUnit documentation says, it should be correctly used to handle the response of certain methods in the chained call.

0 comments voice your opinion now!
phpunit mock method chain object tutorial matcher

Link: http://blog.jmoz.co.uk/phpunit-mocking-and-method-chaining

Bob Majdak:
On SQL in PHP
May 16, 2013 @ 10:11:29

In a new post to his site Bob Majdak looks at using SQL in PHP and some of the challenges he's come across (some of them with his own tools). He talks about things line inline SQL, loading SQL by unique key or creating a "build object".

There is no right or wrong way, but no matter what there is no *pretty* way to do SQL inside of a PHP application. I have been having a personal debate with myself all week about how to make SQL statements nicer in an application without going to a huge DBAL package like Doctrine.

He looks at each idea and provides some of the pros and cons about each of them, noting that he hasn't quite decided on which is the best method. Some sample code is included to help clarify the points, showing the "find by unique key" version and how a more complex query might be created with the "builder object."

0 comments voice your opinion now!
sql load unique key build object pros cons method inline

Link: http://catch404.net/2013/05/on-sql-in-php


Community Events





Don't see your event here?
Let us know!


code series testing framework refactor podcast symfony2 list install language interview threedevsandamaybe developer unittest api release introduction opinion laravel community

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework