Companion Pro PHP Refactoring Pro Pro PHP Refactoring is about improving the quality of the PHP code

download Companion Pro PHP Refactoring Pro Pro PHP Refactoring is about improving the quality of the PHP code

of 359

  • date post

    28-Jun-2020
  • Category

    Documents

  • view

    0
  • download

    0

Embed Size (px)

Transcript of Companion Pro PHP Refactoring Pro Pro PHP Refactoring is about improving the quality of the PHP code

  • Trucchia Rom

    ei PHP Refactoring

    Companion eBook

    Available

    Books for professionals By professionals®

    Francesco Trucchia

    Shelve in: PHP

    User level: Intermediate–Advanced

    THE APRESS ROADMAP

    PHP Object-Oriented Solutions

    PHP for Absolute Beginners

    Beginning PHP and MySQL,

    Third Edition

    Pro PHP: Patterns, Frameworks,

    Testing, and More

    Pro PHP Refactoring

    Pro PHP and jQuery

    PHP Objects, Patterns, and Practice,

    Third Edition

    Practical Web 2.0 Applications with PHP

    www.apress.com SOURCE CODE ONLINE

    Pro PHP Refactoring Pro PHP Refactoring is about improving the quality of the PHP code you write by identifying typical anti-patterns known as bad smells and addressing them by means of solid and safe step-by-step techniques.

    This book is a bridge connecting the PHP language with the refactoring techniques popularized by Martin Fowler and the agile community. The book starts with popular refactorings applied to PHP, then goes on to demonstrate the use of refactoring tools, techniques, and an extended example demonstrat- ing how to refactor an entire application.

    Many companies and freelancers depend on older high-value PHP software that becomes more difficult to maintain as time goes by. The reasons for this may be poorly designed software or code belonging to the procedural days of PHP, when it was not yet object-oriented. This book provides an intermediate or advanced PHP developer with some effective tools to distill the best quality out of existing lines of code. The reader will learn: • What refactoring is and why you need to refactor code • What test-first development is and why you need to test your code • How to write unit and functional tests with PHPUnit and Selenium Remote Control (RC) • How to detect bad smells in PHP code, and refactor them using automated tests • How to refactor a large procedural application affected by many bad smells

    During the last several years, we learned that code can always be improved and that there is always room for a better design. This book will show you how you can also improve your code and your design as well as assist you in your day to-day work with a complete and reliable reference.

    Francesco Trucchia and Jacopo Romei

    Jacopo Romei

    Pro

    The eXperT’s Voice® in open source

    Francesco Trucchia and Jacopo Romei

    Learn the principles and patterns and tools to improve your PHP code

    Pro PHP Refactoring

    Companion eBook

    See last page for details

    on $10 eBook version

  • Pro PHP Refactoring

    ■ ■ ■ Francesco Trucchia and Jacopo Romei

  • Pro PHP Refactoring

    Copyright © 2010 by Francesco Trucchia and Jacopo Romei

    All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.

    ISBN-13 (pbk): 978-1-4302-2727-4

    ISBN-13 (electronic): 978-1-4302-2728-1

    Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1

    Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

    The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

    President and Publisher: Paul Manning Lead Editor: Michelle Lowman Technical Reviewer: Aaron Saray Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell,

    Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh

    Coordinating Editor: Anita Castro Copy Editor: Mary Ann Fugate Compositor: Mary Sudul Indexer: Julie Grady Artist: April Milne Cover Designer: Anna Ishchenko

    Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders- ny@springer-sbm.com, or visit www.springeronline.com.

    For information on translations, please e-mail rights@apress.com, or visit www.apress.com.

    Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales.

    The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.

    The source code for this book is available to readers at www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code.

  • iii

    Contents at a Glance

    Contents................................................................................................................... iv About the Authors................................................................................................ xviii About the Technical Reviewer ............................................................................... xix

    Acknowledgments .................................................................................................. xx

    Introduction ........................................................................................................... xxi

    ■ Chapter 1: Introduction........................................................................................1 ■ Chapter 2: Finding “Bad Smells” in Code ............................................................5

    ■ Chapter 3: Introduction to Refactoring ..............................................................25

    ■ Chapter 4: Principles and Rules.........................................................................47 ■ Chapter 5: Test-First Development ....................................................................57 ■ Chapter 6: Refactoring Tools .............................................................................63 ■ Chapter 7: Structuring Behavior ........................................................................85 ■ Chapter 8: Changing Class Responsibilities.....................................................107 ■ Chapter 9: Dealing with Data Rationalization ..................................................133 ■ Chapter 10: Reducing to Essential Conditional Executions..............................167 ■ Chapter 11: Simplifying Method Calls..............................................................187 ■ Chapter 12: Simplifying Generalization Relationships.....................................211 ■ Chapter 13: Legacy Code .................................................................................251

    ■ Chapter 14: Regression Tests ..........................................................................265 ■ Chapter 15: Refactoring with Patterns ............................................................277

    Index.....................................................................................................................329

  • ■ CONTENTS

    iv

    Contents

    Contents at a Glance................................................................................................iii About the Author ................................................................................................. xviii About the Technical Reviewer ............................................................................... xix Acknowledgments .................................................................................................. xx

    Introduction ........................................................................................................... xxi

    ■ Chapter 1: Introduction........................................................................................1 Lesson Learned ...............................................................................................................2

    Hidden Gems ..........................................................................................................................................2

    You Don’t Know What You’ve Got ’til It’s Gone .......................................................................................2

    Call of Duty ......................................................................................................................3

    ■ Chapter 2: Finding “Bad Smells” in Code ............................................................5

    Why Code Can Smell.................................................................................